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

Mapped inference types do not work as I understand #21765

Closed
AlexStacker opened this issue Feb 8, 2018 · 4 comments
Closed

Mapped inference types do not work as I understand #21765

AlexStacker opened this issue Feb 8, 2018 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@AlexStacker
Copy link

Directly show my code, the text description may not be clear.
From the callback function used in the parameters

TypeScript Version: 2.6.2

Code

interface MapType { 
    a: {
        w: number;
        h: number;
    }
    b: {
        s: string;
        n: boolean;
    }
    q: {
        s: string;
        n: boolean;
    }
}

type NS<T, P extends keyof T> = {
    [U in keyof T[P]]: T[P][U] 
}

let n: NS<MapType, 'a' | 'b'>
n.s //Property 's' does not exist on type 'NS<M, "a" | "b">'.
let m: NS<MapType, 'a'>
m.h // (property) h: number

Expected behavior:

let n: NS<MapType, 'a' | 'b'>
n.s // string
n.n // boolean
n.w // number
n.h // number

Actual behavior:

let n: NS<MapType, 'a' | 'b'>
n.s //Property 's' does not exist on type 'NS<M, "a" | "b">'.

Playground Link:

Related Issues:
I think this is very helpful to me,But did not solve my problem.
17930

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 8, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Feb 8, 2018

I think you want NS<MapType, "a"> & NS<MapType, "b">

@mhegazy
Copy link
Contributor

mhegazy commented Feb 8, 2018

You might have better luck sharing this on StackOverflow. This is not a support forum, it is one for reporting compiler/language bugs and suggestions.
If you believe you are running into a compiler bug, please share a minimal repro project that demonstrates the issue you are running into and why you believe this is a compiler bug and not a declaration file issue or library usage problem.

@AlexStacker
Copy link
Author

AlexStacker commented Feb 9, 2018

Here's the code that can work:

  type NS<T, P extends keyof T> = {
    [U in keyof T]: T[P]
  }

but I want to get their flattened values because I have this code:

function getFieds({
    a: true,
    q: true
  }, function(res) {
    res.s // string
    res.n // boolean
    res.w // number
    res.h // number
  });

or

function getFieds(['a', 'q'], function(res) {
    res.s // string
    res.n // boolean
    res.w // number
    res.h // number
  });

The first argument's subkey is unknown. I need to write its declaration file for this js function.But I think this should also work it:

type NS<T, P extends keyof T> = {
    [U in keyof T[P]]: T[P][U] 
}

function getFieds<T>(options: T, callback: (res: NS<MapType , keyof T>) => void ): void;

It typescript can solve this problem it?

@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants