Skip to content

strict-optional-properties error: Type 'Window & typeof globalThis' is not assignable to type 'Window' #44417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amcasey opened this issue Jun 3, 2021 · 3 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@amcasey
Copy link
Member

amcasey commented Jun 3, 2021

Bug Report

πŸ”Ž Search Terms

strictoptionalproperties, global

⏯ Playground Link

Playground link with relevant code (and enable strictOptionalProperties)

πŸ’» Code

const w: Window = window;

πŸ™ Actual behavior

test.ts:1:7 - error TS2322: Type 'Window & typeof globalThis' is not assignable to type 'Window'.
  Types of property 'ontouchcancel' are incompatible.
    Type '(((this: GlobalEventHandlers, ev: TouchEvent) => any) & ((this: Window, ev: TouchEvent) => any)) | null | undefined' is not assignable to type '((this: GlobalEventHandlers, ev: TouchEvent) => any) | null'.
      Type 'undefined' is not assignable to type '((this: GlobalEventHandlers, ev: TouchEvent) => any) | null'.

1 const w: Window = window;
        ~

πŸ™‚ Expected behavior

Usually, T & U is assignable to T.

@DanielRosenwasser
Copy link
Member

CC @ahejlsberg - it is kind of weird that optional properties on Window don't have corresponding optionality on globals (#36057).

It sounds like the "easiest" workaround is just to mark all the optional properties/methods in the DOM as | undefined.

@amcasey
Copy link
Member Author

amcasey commented Jun 3, 2021

I think fixing the new errors in https://github.com/mui-org/material-ui might be a useful exercise. It looks like there are going to be some issues for consumers of @types/react if we don't seed an update.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 11, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.4.1 (RC) milestone Jun 11, 2021
@sandersn sandersn changed the title New error: Type 'Window & typeof globalThis' is not assignable to type 'Window' strict-optional-properties error: Type 'Window & typeof globalThis' is not assignable to type 'Window' Jun 16, 2021
@sandersn
Copy link
Member

sandersn commented Aug 6, 2021

Fixed by microsoft/TypeScript-DOM-lib-generator#1092 and subsequently #45324

@sandersn sandersn closed this as completed Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants