Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by evmar on Friday June 17, 2016 @07:16PM
(#11926122)
I work with TypeScript fairly closely and was surprised at your example, because I was sure that this already works. It turns out the type narrowing is actually special-cased on only some particular AST. You can see it working here, where "charAt" would not be allowed if it was still |SomeInterface:
http://www.typescriptlang.org/play/#src=interface%20SomeInte...
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by jdmichal on Tuesday June 28, 2016 @10:56AM
(#11994252)
So I started exploring this again today, and it ends up that what you had works, but if I parens on the typeof check, it no longer works:
if (typeof(x) === "string")
I opened an issue: https://github.com/Microsoft/TypeScript/issues/9391Thanks for the help!
This has a very annoying side effect of never being able to exclude interfaces from a union type:
Also, this is ignoring the fact that you would also need to check the parameter types on an incoming function, which AFAIK isn't possible at all.