Skip to content

Type 'unknown' should no be assignable to type '{}' #36485

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
feeddageek opened this issue Jan 28, 2020 · 4 comments
Closed

Type 'unknown' should no be assignable to type '{}' #36485

feeddageek opened this issue Jan 28, 2020 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@feeddageek
Copy link

feeddageek commented Jan 28, 2020

Typescript no longer preventing assignation of unknown to {} without strictNullChecks

TypeScript Version: from 3.5.3 to 3.7.5
Work as expect from 3.0.3 to 3.4.5

Search Terms: unknown assignable {} empty type

Code

    let a: unknown;
    const b: {} = a;
    const c: object = a;

Expected behavior:

    let a: unknown;
    const b: {} = a;// Type 'unknown' is not assignable to type '{}'.
    const c: object = a;// Type 'unknown' is not assignable to type 'object'

Actual behavior:

    let a: unknown;
    const b: {} = a;// No error for this line
    const c: object = a;// Type 'unknown' is not assignable to type 'object'

Playground Link:
3.3.3 strictNullChecks=true Error as expected
3.3.3 strictNullChecks=false Error as expected
3.7.5 strictNullChecks=true Error as expected
3.7.5 strictNullChecks=false Missing error
Related Issues:
#26796

@DanielRosenwasser
Copy link
Member

If strictNullChecks is off, {} is implicitly {} | null | undefined, which encompasses the same set of values as unknown. I'm not totally clear on why that should change.

Thoughts @ahejlsberg?

@feeddageek
Copy link
Author

If strictNullChecks is off, {} is implicitly {} | null | undefined, which encompasses the same set of values as unknown. I'm not totally clear on why that should change.

Does it ?
3.0 change-log specifically state :

// unknown assignable only to itself and any
let v6: {} = x; // Error
let v7: {} | null | undefined = x; // Error

There is nothing specifying that it's only true with strictNullChecks (and no other change-log between 3.0 and 3.7 defines this change in expected behavior)

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 31, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@feeddageek
Copy link
Author

@RyanCavanaugh It is not working as intended. There has been an undocumented change in behaviour between 3.3 and 3.4. Even if it was intended (or a desirable accident) and it doesn't need fixing, it need to be documented.

The 3.0 changlog clearly state that this assignation should not be permitted and I can't find any indication that it should have changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants