You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classA{a: string;}classBextendsA{b: string;}functionisB(x: {}): x is B{returnxinstanceofB;}constisA: (x: {})=>x is A=isB;// allowed, should be compile errorconstx=<A|string>newA();if(!isA(x)){console.log(x.slice());// runtime error}
Expected behavior: Assignability error where marked.
Actual behavior: Successful compilation, runtime error.
If we want a kind of type guard function that is covariant, we need to not narrow when it returns false, i.e., a "true" result would be sufficient but not necessary for the value to be of the tested type. And we'd need a different syntax for the type of a type guard function that is necessary and sufficient compared to a type guard function that is sufficient but not necessary. One idea for the latter is (x: {}) => x is A | false. (A type guard function that is necessary but not sufficient would then be (x: {}) => x is A | true.) Getting all these variants supported would help us support conjunctions and disjunctions of type guard calls with other boolean expressions in #24865. If you like, this issue can be for the removal of the unsound assignability rule and I can file a separate suggestion for the new kinds of type guard functions.
TypeScript Version: master (af8e44a)
Search Terms: user-defined type guard predicate assignable assignability unsound covariant invariant
Code
Expected behavior: Assignability error where marked.
Actual behavior: Successful compilation, runtime error.
Playground Link: link
Related Issues: #24865
If we want a kind of type guard function that is covariant, we need to not narrow when it returns false, i.e., a "true" result would be sufficient but not necessary for the value to be of the tested type. And we'd need a different syntax for the type of a type guard function that is necessary and sufficient compared to a type guard function that is sufficient but not necessary. One idea for the latter is
(x: {}) => x is A | false. (A type guard function that is necessary but not sufficient would then be(x: {}) => x is A | true.) Getting all these variants supported would help us support conjunctions and disjunctions of type guard calls with other boolean expressions in #24865. If you like, this issue can be for the removal of the unsound assignability rule and I can file a separate suggestion for the new kinds of type guard functions.