Skip to content
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

Apparent type of unknown when strictNullChecks=false causes odd errors on property access #47494

Open
jakebailey opened this issue Jan 18, 2022 · 1 comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript

Comments

@jakebailey
Copy link
Member

When strictNullChecks is disabled (as in JS code), destructuring a variable of type unknown produces the message:

Property 'foo' does not exist on type '{}'.(2339)

Whereas with it enabled, the error is:

Property 'foo' does not exist on type 'unknown'.(2339)

This behavior isn't actually tested anywhere, and was revealed in #47442.

When assigning unknown to a variable of some class type with properties, a similar error is produced:

Property 'prop' is missing in type '{}' but required in type 'A'.(2741)

Whereas strictNullChecks = true produces a more obvious error:

Type 'unknown' is not assignable to type 'A'.(2322)

This behavior (and other errors like it) is actually captured in a few tests.

This all seems to stem from the fact that getApparentType returns the empty object type as unknown's apparent type, which was added in #30637.

I would personally argue that the latter errors in the above examples make more sense than referring to {}, which doesn't actually appear in the code anywhere. Right now, this doesn't seem to be very visible (as people in JS are unlikely to see unknown), but with #47383 / #47442, this may become more visible.

const { foo } = {} as unknown;

function func({ foo }: unknown) {}

try {} catch ({ foo }) {}


class A {
    prop: number;
}

const a: A = {} as unknown;

Playground Link

Split off from #47442 (comment).

@jakebailey
Copy link
Member Author

Now that I think a bit harder, this message won't show up in JS without checkJs, so maybe this isn't a huge deal after all, but it is a little awkward if anyones's running with strictNullChecks off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants