8000 `never` incorrectly inferred with type guard · Issue #10934 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
never incorrectly inferred with type guard #10934
@paldepind

Description

@paldepind

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 {
  co
6DF0
nstructor() {};
}

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'.

Metadata

Metadata

Assignees

Labels

FixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0