Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the type {} inferred instead of unknown when strictNullChecks is disabled?

Consider the following code:

const { name } = "something" as unknown;

When I enable Typescript’s strictNullChecks, I get the following error message:

Error: Property `name` does not exist on type `unknown`

Which sounds about right.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But when I disable strictNullChecks, I get the following error message:

Error: Property `name` does not exist on type `{}`

What does {} mean here?

I only know {} as non-nullish value. But why is it inferred here instead of unknown?

>Solution :

What does {} mean here? I only know {} as non-nullish value. But why is it inferred here instead of unknown?

When strictNullChecks is false, null and undefined might as well not exist; they’re ignored by the language. As you said {} means a non-nullish value, and if null and undefined don’t exist, that’s everything. So when strictNullChecks are turned off, {} and unknown are equivalent.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading