Closed as not planned
Closed as not planned
Description
π Search Terms
collapsed union, instanceof, structural types, nominal types
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about nominal types and instanceof
β― Playground Link
π» Code
class Foo<T> {
constructor(readonly x: T) {}
withX<T>(arg: T): Foo<T> {
return new Foo(arg);
}
}
class Bar<T> {
constructor(readonly x: T) {}
withX<T>(arg: T): Bar<T> {
return new Bar(arg);
}
}
function fooOrBar<T>(arg: T): Foo<T>|Bar<T> {
return new Bar(arg);
}
function assertString(x: string) {
if (typeof x !== 'string') throw new Error('eek!');
}
function f(): void {
const x = fooOrBar(42);
const y = x.withX(1);
if (y instanceof Foo) {
return;
}
assertString(y); // y is never, so this type checks OK
}
π Actual behavior
assertString
call gives no error
π Expected behavior
assertString
gives an error, since neither Foo
nor Bar
are assignable to string
Additional information about the issue
This appears somewhat related to #202 (and having a way to force classes to be treated nominally would have avoided this issue for us). However, this issue does seem distinct in that it has a missing error even when classes are interpreted structurally.