Skip to content

anything is assignable to T[P] where T extends null/undefined/void/object/never #15371

Description

case 1

exportfunctiontest<Textendsobject,PextendskeyofT>(whatever: T,name: P): T[P]{constresult=Math.random()>0.5 ? whatever[name] : 'Hello!';// <-- expected T[P] | string, actual T[P]returnresult;// <-- returning unsound result}

case 2

exportfunctiononeOrAnother<A,B>(one: A,another: B): A|B{returnMath.random()>0.5 ? one : another;}exportfunctiontest<Textendsobject,PextendskeyofT>(whatever: T,name: P): T[P]{constresult=oneOrAnother(whatever[name],'Hello!');// <-- result is clearly string | T[P] as expectedreturnresult;// <-- expected a type error, actual returning string | T[P] instead of T[P] no problem}

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions