-
Notifications
You must be signed in to change notification settings - Fork 12.8k
strictNullChecks - property accesses on string index signatures should narrow #17960
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
Comments
The second issue is tracked by #13778. |
It's also surprising that assigning the (theoretically) type narrowed object to a different variable removes the error: if (process.env.MY_ENV_VAR) {
console.log(process.env.MY_ENV_VAR.length); // ERROR
}
const v = process.env.MY_ENV_VAR;
if (v) {
console.log(v.length); // NO ERROR
}
|
Closed
Another seemingly related error (TS 3.4.5): declare const foo: {
[id: string]: {
bar?: {
baz?: boolean;
}
}
};
const a1 = foo.id && foo.id.bar && foo.id.bar.baz; // works fine
const key = 'id';
const a2 = foo[key] && foo[key].bar && foo[key].bar.baz; // error: Object is possibly 'undefined' |
Just looking at old issues; this one was fixed by #19838. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 2.6.0-dev.20170819
Code
Expected behavior:
When using strictNullChecks :
TS2532: Object is possibly 'undefined'
error when accessing an index of an object defined as{ [key: string]: string | undefined }
inside a guard.{ [key: string]: string }
inside a guard without checking it first.Actual behavior:
Please see code sample.
The text was updated successfully, but these errors were encountered: