Skip to content

Optional Chaining Edge Case #43748

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
awjreynolds opened this issue Apr 20, 2021 · 6 comments
Closed

Optional Chaining Edge Case #43748

awjreynolds opened this issue Apr 20, 2021 · 6 comments

Comments

@awjreynolds
Copy link

Bug Report

Optional chaining is allowing undefined to be assigned to a variable that does not allow undefined.

πŸ”Ž Search Terms

optional chaining

πŸ•— Version & Regression Information

3.8.3
4.2.3
4.3.0

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.3.0-beta#code/PTAEHUFMBsGMHsC2lQBd5oBYoCoE8AHSAZVgCcBLA1UABWgEM8BzM+AVwDsATAGiwoBnUENANQAd0gAjQRVSQAUCEmYKsTKGYUAbpGF4OY0BoadYKdJMoL+gzAzIoz3UNEiPOofEVKVqAHSKymAAmkYI7NCuqGqcANag8ABmIjQUXrFOKBJMggBcISGgoAC0oACCoASMFmgY7p7ehCTkVOle4jUMdRLYTqCc8LEZzCZmoNJODPHFZZXVtZYYkAAeRJTInDQS8po+rf40gnjbDKv8LqD2jpbYoACqAEoAMsK7sUmxkGSCc+VVQQuaTwVb1UBrDYULY7PagbgUZLJH6QbYmJAECjuMigZEMVDsJzCFLNXxtajBBCcQQ0MwAUVWDEQNUgADVHBQGNJ3KAALygABEAAkYNAMOB4GRogLFFTBPB3AExcwABT0xnM9zsyhc9wASkp8GpNAUNPy11QlE4zAA2gBde180D2gDcsqNNLQ+lQACZzTSrba7U7TagbQAGO0AfgCiAYBBVnCZKF5AD5Bsm9W65QrIEr4KrQz6s8EVDgGh4yF5EJLnCD2Cb7oxrewGMxIPxYNB1IkufA9CIvAKGUyWYIBUkcQLwA5UAByYQAOUgEgFQRUAHlvmRdoIO1pIMdUI4FK5pHhQE4azpRlh9CgEIgYcIrt9JJLoiIDBwcd0WGwuG4IISiAA

πŸ’» Code

const test2: string[] = test[0]?.map(name => name);
console.log(test2);

πŸ™ Actual behavior

No errors reported.

πŸ™‚ Expected behavior

test2 assignment should be flagged as an error as optional chaining can return undefined.

@whzx5byb
Copy link

test[0]?.map(name => name) is equivalent to test[0].map(name => name) here, because test[0] is always string[].

To produce the expected behavior, set noUncheckedIndexedAccess to true.

@awjreynolds
Copy link
Author

test is an empty array and test[0] is undefined. The optional chaining stops at that point and returns undefined. test2: string[] | undefined = test[0]?.map(name => name) is the only way this should not be an error.

@whzx5byb
Copy link

test is an empty array and test[0] is undefined. The optional chaining stops at that point and returns undefined. test2: string[] | undefined = test[0]?.map(name => name) is the only way this should not be an error.

The compiler doesn't know test is an empty array. It only knows test is of type string[][].
Also, any index access of T[] will return T without noUncheckedIndexedAccess.
So in your example, the type of test[0] is string[] instead of string[] | undefined.

@awjreynolds
Copy link
Author

My issue is that Optional Chaining can return undefined. The following correctly identifies the error:

const test=  (): string[] | undefined => undefined;
const test2: string[] = test(); // error

What I'm saying is that if you use optional chaining for assignment then the result can be a type or undefined and this is not picked up by Typescript for this specific scenario.

@nmain
Copy link

nmain commented Apr 20, 2021

Besides noUncheckedIndexedAccess, you might also be interested in #36672.

@awjreynolds
Copy link
Author

I think that covers the issue and I can close this. I didn't search hard enough to find it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants