Skip to content

Allow type parameter constraints to reference other type parameters #2304

Description

@NN---

I would like to have some feature that infers any possible types from the given object in the generic function.
For instance I have a function that receives Object, and I want to have a more specific callback than 'any'.
Current code:

functiona(obj: Object,f: (value: any)=>any){for(varpinobj)if(obj.hasOwnProperty(p)){(<any>obj)[p]=f((<any>obj)[p])}}a({x:1},(value: any)=>(<number>value)+1);

I wish I had something with more strict typing.

functiona<TextendsObject,Value: union_of_properties_types_of(T)>(obj: T,f: (value: Value)=>Value){for(varpinobj)if(obj.hasOwnProperty(p)){(<any>obj)[p]=f(<Value>(<any>obj)[p])}}// 'f' is denoted to number => numbera({x:1},value=>value+1);// 'f' is string => stringa({x:"x",y:"y"},value=>{returnvalue+"z";});

Metadata

Metadata

Labels

CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions