We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
TypeScript Version: 3.1.0-dev.20180822
Search Terms: Object is possibly 'undefined'. strictNullChecks
Code
const objA = { a: 1, b: 2 }; type Key = keyof typeof objA; const objB = {} as { [index in Key] ? : string[] }; for (const key of Object.keys(objA) as Key[]) { objB[key] = ['x']; console.log(objB[key][0]); }
Expected behavior: No error.
Actual behavior:
error TS2532: Object is possibly 'undefined'. 6 console.log(objB[key][0]); ~~~~~~~~~
Playground Link: https://www.typescriptlang.org/play/index.html#src=const%20objA%20%3D%20%7B%20a%3A%201%2C%20b%3A%202%20%7D%3B%0D%0Atype%20Key%20%3D%20keyof%20typeof%20objA%3B%0D%0Aconst%20objB%20%3D%20%7B%7D%20as%20%7B%20%5Bindex%20in%20Key%5D%20%3F%20%3A%20string%5B%5D%20%7D%3B%0D%0Afor%20(const%20key%20of%20Object.keys(objA)%20as%20Key%5B%5D)%20%7B%0D%0A%20%20objB%5Bkey%5D%20%3D%20%5B'x'%5D%3B%0D%0A%20%20console.log(objB%5Bkey%5D%5B0%5D)%3B%0D%0A%7D
The text was updated successfully, but these errors were encountered:
Control flow analysis only works for known keys (like obj.x) and not computed keys. So it won't work for an object key stored in a variable.
obj.x
Sorry, something went wrong.
Thanks.
tsconfig -> strictNullChecks:false
No branches or pull requests
TypeScript Version: 3.1.0-dev.20180822
Search Terms:
Object is possibly 'undefined'.
strictNullChecks
Code
Expected behavior:
No error.
Actual behavior:
Playground Link:
https://www.typescriptlang.org/play/index.html#src=const%20objA%20%3D%20%7B%20a%3A%201%2C%20b%3A%202%20%7D%3B%0D%0Atype%20Key%20%3D%20keyof%20typeof%20objA%3B%0D%0Aconst%20objB%20%3D%20%7B%7D%20as%20%7B%20%5Bindex%20in%20Key%5D%20%3F%20%3A%20string%5B%5D%20%7D%3B%0D%0Afor%20(const%20key%20of%20Object.keys(objA)%20as%20Key%5B%5D)%20%7B%0D%0A%20%20objB%5Bkey%5D%20%3D%20%5B'x'%5D%3B%0D%0A%20%20console.log(objB%5Bkey%5D%5B0%5D)%3B%0D%0A%7D
The text was updated successfully, but these errors were encountered: