Skip to content

Incorrect any type distilled from extended generic after typeof check #41797

Open
@dennis-c

Description

@dennis-c

TypeScript Version: 4.1.2 and 4.2.0-dev.20201202

Search Terms: extended generic typeof any

Code

type Identifier = string;

interface Identifiable {
	readonly id: Identifier;
}

class DropdownItem<ID extends Identifier> implements Identifiable {
	public readonly id: ID;
	public readonly name: string;

	public constructor(id: ID, name: string) {
		this.id = id;
		this.name = name;
	}
}

const defaultItemGetCells = (item: Identifiable): string[] => {
	if (item instanceof DropdownItem) {
		return [ item.id, item.name ]; //item.id is incorrectly typed as any
	} else {
		return [ item.id ]; //item.id is correctly typed as string
	}
};

Expected behavior:
The type of item.id on line 19 is correctly identified as a string (just like on line 21). It should always be at least a string, even is no generic is passed to DropdownItem (And I can't pass a generic because it's plain old javascript). If I remove the name field from the DropdownItem, it infers correctly.

Actual behavior:
The type of item.id on line 19 is incorrectly identified as any, this causes our typescript-eslint no any rule to kick in.
Screenshot 2020-12-03 at 08 58 56

Playground Link: Example

Related Issues: I could find nothing that looked similar

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions