- Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Motivating example (one of many):
functioncompare<T>(lhs: T,rhs: T): boolean{returnlhs===rhs;}if(compare('1',1)){// Expected: Error -- I made 'compare' generic for a reason!/* ... */}Proposal
When generic type inference performs its Best Common Type operation (4.12.2), it should be an error if this returns {} when {} was not one of the input types.
This is entirely consistent with other behavior already in the compiler:
varfoo=bar ? '1' : 1;// Error, no BCT between '1' and 1functionfn(){// Error, no BCT between '1' and 1if(foo){return'1';}else{return1;}}Open Questions
From @KamyarNazeri -- should this apply when there are zero input types? e.g.:
classList<T>{items: T[];}varx=newList();// Probably want an error hereThat seems desirable, but has some collateral damage:
functionfoo<T>(x: number,a?: T,b?: T){}foo(0);// Error, zero input types provided for inferenceThis would come up slightly more often than naively expected because many .d.ts authors (mistakenly) create generic types which don't actually consume their type parameters. Perhaps this warning would effectively discourage them from doing so?
Metadata
Metadata
Assignees
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript