-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Generic lookup on mapped types cannot resolve type #29528
Comments
I'd be interested in your use-case here. It can also be written the following way, without errors: function getIdentity<T extends letters>(t: T): typeof identityMapping[T] {
return identityMapping[t];
} And also another instance improved by #29049. |
This is a common pattern we use, for instance to assign a render function to every member of an enum set: type letters = 'a' | 'b'
const renderMapping: {
[l in letters]: renderFunction<l>;
} = {
'a': (a: 'a') => 'alpha',
'b': (b: 'b') => 'bravo',
};
type renderFunction<l extends letters> = (letter: l) => string;
function renderLetter<l extends letters>(letter: l): renderFunction<l> {
return renderMapping[letter];
} I'm trying to understand if this is a bug, or a limitation of typescript by design. If it is a limitation, what is that limitation and what is the workaround? You can see the type error generated in this playground: |
I would say that this is by design. Your are indexing into something concrete (with no generic parameters) and expecting to get back something generic; specifically, accessing into type letters = 'a' | 'b'
type RenderMap = { [l in letters]: RenderFunction<l>;}
const renderMapping: RenderMap = {
'a': (a: 'a') => 'alpha',
'b': (b: 'b') => 'bravo',
};
type RenderFunction<l extends letters> = (letter: l) => string;
function renderLetter<l extends letters>(letter: l): RenderMap[l] {
return renderMapping[letter];
} |
As usual I agree with @jack-williams. Basically TypeScript would need to be able to do second-order reasoning about generics (i.e. figuring out that the correlation within the mapped type was equivalent to the correlation in the function signature) in order to understand that the return expression actually did match the return type, but we're really only capable of first-order reasoning. |
TypeScript Version: 3.2.0
Search Terms: Mapped Types, Generics
Code
Expected behavior:
Compiles without type errors
Actual behavior:
Playground Link:
http://www.typescriptlang.org/play/#src=type%20letters%20%3D%20'a'%20%7C%20'b'%0D%0A%0D%0Aconst%20identityMapping%3A%20%7B%0D%0A%20%20%5Bletter%20in%20letters%5D%3A%20letter%0D%0A%7D%20%3D%20%7B%0D%0A%20%20'a'%3A%20'a'%2C%0D%0A%20%20'b'%3A%20'b'%0D%0A%7D%3B%0D%0A%0D%0Afunction%20getIdentity%3CT%20extends%20letters%3E(t%3A%20T)%3A%20T%20%7B%0D%0A%20%20return%20identityMapping%5Bt%5D%3B%0D%0A%7D%0D%0A
Related Issues:
The text was updated successfully, but these errors were encountered: