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

TS2339: Property 'bar' does not exist on type 'never' (strictNullChecks) #41766

Closed
maksimr opened this issue Dec 1, 2020 · 5 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@maksimr
Copy link

maksimr commented Dec 1, 2020

Example code:

index.js

const [foo] = [null];
console.log(foo?.bar);

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "allowJs": true,
    "checkJs": true,
    "noEmit": true,
    "strictNullChecks": true,
    "moduleResolution": "node",
    "resolveJsonModule": true
  }
}

Command:

tsc

AR:

error TS2339: Property 'bar' does not exist on type 'never'.

2 console.log(foo?.bar);
                   ~~~
Found 1 error.

ER: No error


Typescript version 4.1.2

If change the code on:

const bar = [null];
const foo = bar[0];
console.log(foo?.bar);

All works fine

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 1, 2020
@RyanCavanaugh
Copy link
Member

By what justification should this not be an error? This code doesn't make any sense.

@maksimr
Copy link
Author

maksimr commented Dec 1, 2020

@RyanCavanaugh this is just a minimal reproducible example without any attached business logic to the code. The issue that typescript throws error when we handle null value.
Code like this

const bar = [null];
const foo = bar[0];

does not throw error.

In real world use case we use useState with initial value null and after that we set an object. We do the same null check but typescript throw error and this happens only when destruction is used

P.S.
Example of code similar to setState even with explicit type definition and without destruction

/**
 * @type {{bar:string}|null}
 */
let foo = null;
setState();
console.log(foo?.bar);

function setState() {
  foo = {bar: 'zoo'};
}

This code still throw

Property 'bar' does not exist on type 'never'.

6 console.log(foo?.bar);

@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.

@RyanCavanaugh
Copy link
Member

Seems like you're talking about #9998 then

@maksimr
Copy link
Author

maksimr commented Dec 4, 2020

@RyanCavanaugh kind of... mostly about optional chaining foo?.bar and strict null check. What I want is eliminate Property '<name>' does not exist on type 'never'. error without disabling strict null check. For me in the code above chaining operator should be enough to say typescript analyzer that I know about possible null value and cover it by optional chaining operator ? so all null safe :)
Thanks

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

3 participants