-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Type guard using square bracket notation does not narrow type #28081
Comments
Is this a duplicate of #10530? @andy-ms @weswigham |
No, this is not a duplicate. It's a missed interaction between indexed access and index signatures, where property access + index signatures works. interface ConfigType {
[key: string]: boolean | { prop: string };
}
const config: ConfigType = {
works: { prop: 'foo' }
};
if (typeof config.works !== 'boolean') {
config['works'].prop = 'test'; // error, config['works']: boolean | { 'prop': string }
config.works.prop = 'test'; // ok
} The behaviour is the same if the discriminator is Also note that if we have |
Just a drive by comment. We are working in Google internal TS to forbid using the prop access on index signature objects. The main reason is that to Closure prop access means safe to rename and index access means don't touch. This optimization pass is done without global knowledge (other than whats' coming externally) so its up to the developer to decide whether it is really "safe" to rename. The transformation from |
Turns out this was just missing. It is super simple to add since it's the same code as property access. I just need to make sure performance is OK. |
Update: The PR is trivial and performance is, as expected, bad for probably-uncommon adversarial code. We need to discuss in the design meeting whether to take this change. |
TypeScript Version: 3.2.0-dev.20181023
Search Terms:
type guard square bracket notation narrowing
Code
Expected behavior:
The type guard using square bracket notation narrows the type in the same way the type guard using dot notation does, resulting in no type errors in the above example.
Actual behavior:
The type guard specifically using square bracket notation does not narrow the type.
Playground Link:
Link
Related Issues:
#10530
The text was updated successfully, but these errors were encountered: