Wrong keyof behaviour for generic with extends types in 2.9
See original GitHub issueTypeScript Version: 2.9.1, 3.0.0-dev.20180601
Search Terms: keyof 2.9 generics extract extends string number symbol
Code
type StringKeyof<T> = Extract<keyof T, string>;
type Omit<T, K extends StringKeyof<T>> = any;
type WithoutFoo = Omit<{ foo: string }, "foo">; // ok
type WithoutFooGeneric<P extends { foo: string }> = Omit<P, "foo">; // Error: Type '"foo"' does not satisfy the constraint 'Extract<keyof P, string>'.
Expected behavior:
Omit
should pass "foo"
as valid type for keyof because generic type extends a type that has this string key.
Actual behavior:
Error: Type '"foo"' does not satisfy the constraint 'Extract<keyof P, string>'.
Playground Link: Link
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:10 (3 by maintainers)
Top Results From Across the Web
TypeScript Wrong typechecking when remapping keys with ...
We're trying to use conditional types to filter a generic type related to keyof a generic type, and the compiler cannot figure out...
Read more >Documentation - Generics - TypeScript
This allows us to traffic that type information in one side of the function and out the other. We say that this version...
Read more >Generic and keyof - Learn TypeScript - Educative.io
You can use extends to extend a generic type to another type if you want the generic type to be a subset of...
Read more >TypeScript Types Deep Dive 3 - Generics and Advanced Types
It includes: * generics * how to DRY your types with advanced types : index access, keyof, typeof, Next part will include: mapped...
Read more >TypeScript — Confusing Concepts and Usage | by E.Y. | Medium
Sometimes we need to type assert a generic value into a literal type. ... Generic Type using extends , such as <T, K...
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
@princefishthrower Your issue is not related to the original issue. I think you might have misunderstood the usage of the keyword
Extract
orkeyof T
.Taking back the interface
ISomething
with slight alteration to the names:Since the keys of
ISomething
are all strings,Extract
returns the full list of keys.My guess is you want only the keys foo, bar or baz, since they map to
are ofordered types. Instead ofExtract
, you might be looking to use the following “FieldOfType” type:Here’s a Playground Link if you want to play with it.
EDIT: The keys map to ordered types instead of are.
@amoscatelli No, the issue there is that conditional types ignore the constraints on type parameters when checking assignability, so
T[K] extends string ? K : never
does not get simplified toK
.