Parameter of a generic interface doesn't work when it's a generic index of an another interface
See original GitHub issueTypeScript Version: typescript@next, typescript@3.5.1
Search Terms: generic index is not assignable
Code
interface SettingsTypes {
audio: {
volume: string;
};
video: {
resulution: string;
}
}
interface Settings<Params extends { [K in keyof Params]?: string }> {
config: Params;
};
// Example 1
type ThisWorks = Settings<SettingsTypes['audio']>;
// Example 2
type ThisDoesntWork<T extends keyof SettingsTypes> = Settings<SettingsTypes[T]>;
// ERROR:
// Type '{ volume: string; }' is not assignable to type '{ [K in keyof SettingsTypes[T]]?: string | undefined; }'.
// oh, ok but then why this works?
// Example 3
type ThisWorksAgain = Settings<{ volume: string; }>;
Expected behavior: Generic should work the same if we pass an specific index of an interface (1), a generic index of an interface (2) or when we pass the type directly (3)
Actual behavior:
Typescript throws an error only for the construction Settings<SettingsTypes[T]>
.
Related Issues: 32017 and 31904
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Generic interface with index-type property and method
Is there a way in TypeScript to declare the following interface using index types and exactly one generic type, where ...
Read more >Java Generics Example Tutorial - Generic Method, Class ...
If you have been working on Java Collections and with version 5 ... A generic type is a class or interface that is...
Read more >Generics are the Generics of Go | Capital One
Read through the Go Generics Draft (formally called the Type Parameters - Draft Design) to see more details on the draft design, things...
Read more >6.5 Generic units - Ada Resource Association
Remember that the matching of object and subprogram generic parameters is ... we have various interfaces all derived from Printable which serve different...
Read more >11. Generic Lists
where T is a type parameter. This type must be an Object, not a primitive type. Many of the methods reference this type,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@marcingajda
Looking into this abit more, I believe your issue boils down to this:
Basically
keyof SettingsTypes[T] ==> string | number | symbol
, which causes later issues in your example because the checker needs:SettingsTypes[T][string | number | symbol] <: (string | undefined)
per the comment:
but there are no index signatures on
SettingsTypes[T]
.Expanding
T
to its constraint inkeyof SettingsTypes[T]
looks tempting, but I believe it would be unsound:So I would leave this issue open and let someone on the team have a definitive look. I’m glad you have something that works for you though!
Minimal version:
Note that this version works: