Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
richardspence opened this issue Jul 24, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@richardspence
Copy link

richardspence commented Jul 24, 2017

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 TPayload
type Unpromise<TPayload, TPromise extend Promise<TPayload>> = TPayload;

//and then use that also as a type guard for mapped types
type Reducer<TActions> = {
    [K in keyof TActions where TActions[K] extends Promise<any>]: (payload: Unpromise<TActions[K]>)=>void;
    [K in keyof TActions where TActions[K] !extends Promise<any>]: (payload: TActions[K] )=>void;
}

Complete Example

//redux style actions class
class Actions{
    get(): Promise<string>;
}
//redux style reducer
class MyReducer{
  get(payload:string){...}
}

//unpromise type
type Unpromise<TPayload, TPromise extend Promise<TPayload>> = TPayload;

//example derived usage with generic constraints
type Reducer<TActions> = {
    [K in keyof TActions where TActions[K] extends Promise<any>]: (payload: Unpromise<TActions[K]>)=>void;
    [K in keyof TActions where TActions[K] !extends Promise<any>]: (payload: TActions[K] )=>void;
}

//redux style reducer wireup
function handleActions<TActions>(actions: TActions, reducer: Reducer<TActions>){
...
}

//this would check that MyReducer has methods similar to Actions taking in the resolved promise payload as an argument
handleActions(new Actions(), new MyReducer());

related to #12424

@mhegazy
Copy link
Contributor

mhegazy commented Aug 26, 2017

See #12424 and #17077

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 26, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 11, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Sep 11, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants