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
typeIdentifier=string;interfaceIdentifiable{readonlyid: Identifier;}classDropdownItem<IDextendsIdentifier>implementsIdentifiable{publicreadonlyid: ID;publicreadonlyname: string;publicconstructor(id: ID,name: string){this.id=id;this.name=name;}}constdefaultItemGetCells=(item: Identifiable): string[]=>{if(iteminstanceofDropdownItem){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.
Using instanceof on classes containing only public members is not a safe operation. Such classes a structurally typed, so an object literal is implicitly assignable, and in this case instanceof fails "unexpectedly".
Hey, any updates on this one?
I've encountered this issue as well - here is a simplified version of my scenario (which also using instanceof).
@MartinJohns I didn't understand your side-note, the following still fails even if I add a private field to MyClass
interface BaseT {
baseProp: number;
}
export class MyClass<T extends BaseT> {
propA: T;
}
const obj1 = {}; // simpified version, assume factory function which return type any
// should show a compilation error, since baseProp is number, but it doesn't!
// will show compilation error when "propA: T" by "propA: BaseT"
if (obj1 instanceof MyClass && obj1.propA.baseProp === 'aa') {
}
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 4.1.2 and 4.2.0-dev.20201202
Search Terms: extended generic typeof any
Code
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 thename
field from theDropdownItem
, 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.
Playground Link: Example
Related Issues: I could find nothing that looked similar
The text was updated successfully, but these errors were encountered: