I was refactoring some code and suddenly some type guards in my code begin causing type errors. I've reduced my problem to the small example below. I just did npm install -g typescript@next to check that this also happens with the latest version.
After some confusion and debugging it appears that the bug happens because the class Empty has no methods. If I add a random dummy method to Empty the code compiles again.
TypeScript Version: 2.1.0-dev.20160915
Code
export type FingerTree<A> = Empty | Single<A>;
export class Empty {
constructor() {};
}
export class Single<A> {
constructor(
public nested: boolean
) {};
}
export function test(t: FingerTree<any>): boolean {
if (t instanceof Empty) {
return true;
} else {
return t.nested;
}
}
Expected behavior:
Code compiles without errors and t has the type Singe<any> in the else clause in test.
Actual behavior:
t has the type never in the else clause and I get` the compile error:
test.ts(17,14): error TS2339: Property 'nested' does not exist on type 'never'.
I was refactoring some code and suddenly some type guards in my code begin causing type errors. I've reduced my problem to the small example below. I just did
npm install -g typescript@nextto check that this also happens with the latest version.After some confusion and debugging it appears that the bug happens because the class
Emptyhas no methods. If I add a random dummy method toEmptythe code compiles again.TypeScript Version: 2.1.0-dev.20160915
Code
Expected behavior:
Code compiles without errors and
thas the typeSinge<any>in theelseclause intest.Actual behavior:
thas the typeneverin the else clause and I get` the compile error: