Skip to content

[Suggestion] Mapped types with inner generic arguments and constraints #17375

Description

@richardspence

Proposal

I'd like to suggest a feature where in mapped types we could unwrap some of the properties. I'm trying to solve a problem where I want to unpromise a type, and this requires access to the inner generic argument type. This is useful in creating redux style reducers that are strongly typed.

Example

//i'd like access to the inner generic arguments, without having to pass TPayloadtypeUnpromise<TPayload,TPromiseextendPromise<TPayload>>=TPayload;//and then use that also as a type guard for mapped typestypeReducer<TActions>={[KinkeyofTActionswhereTActions[K]extendsPromise<any>]: (payload: Unpromise<TActions[K]>)=>void;[KinkeyofTActionswhereTActions[K]!extendsPromise<any>]: (payload: TActions[K])=>void;}

Complete Example

//redux style actions classclassActions{get(): Promise<string>;}//redux style reducerclassMyReducer{get(payload:string){...}}//unpromise typetypeUnpromise<TPayload,TPromiseextendPromise<TPayload>>=TPayload;//example derived usage with generic constraintstypeReducer<TActions>={[KinkeyofTActionswhereTActions[K]extendsPromise<any>]: (payload: Unpromise<TActions[K]>)=>void;[KinkeyofTActionswhereTActions[K]!extendsPromise<any>]: (payload: TActions[K])=>void;}//redux style reducer wireupfunctionhandleActions<TActions>(actions: TActions,reducer: Reducer<TActions>){
...
}//this would check that MyReducer has methods similar to Actions taking in the resolved promise payload as an argumenthandleActions(newActions(),newMyReducer());

related to #12424

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions