-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type information lost after Pick on object with [key: string]: any; #24185
Comments
Its very makes sense it will behave like that, type YY<T> = { [K in keyof T]: T[K] };
type YYY = YY<X>; |
@Bnaya yes your solution works for my example. But this issue is causing a problem on Omit. I updated the description with my original use case my other finding:
|
Got it. type X = {
[key: string]: any;
x: string;
y: string;
}
type X3 = {
x: string;
y: string;
}
type Omit<T, TR extends keyof T> = {
[K in Exclude<keyof T, TR>]: T[K];
};
/**
Y2 = {
[x: string]: any;
}
*/
type Y2 = Omit<X, "x">;
/**
type Y3 = { y: string; }
*/
type Y3 = Omit<X3, "x">; |
The type has an index signature that allows it to have any string named property.. there are no way int eh language to define exclusion from infinite ranges, e.g. |
@mhegazy I'm expecting |
|
is there a way for Typescript to pick "x" and "y" then? Because during type checking, the definition of "x" and "y" takes precedence |
The compiler will try to keep the properties as long as it can, e.g. |
will it make sense for my use case is to make one required field optional, which is common for React component with higher order components. The only solutions I can find is to use Extract which doesn't work here. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.8.3
Search Terms: Pick
Code
Expected behavior: Y to be same as X
Actual behavior: Y = {[key: string]: any;}
Playground Link:
https://www.typescriptlang.org/play/#src=type%20X%20%3D%20%7B%0D%0A%20%20%20%20%5Bkey%3A%20string%5D%3A%20any%3B%0D%0A%20%20%20%20x%3A%20string%2C%0D%0A%20%20%20%20y%3A%20string%0D%0A%7D%0D%0A%0D%0Atype%20Y%20%3D%20Pick%3CX%2C%20keyof%20X%3E
Related Issues:
My use case is to make a required field optional in type X
The text was updated successfully, but these errors were encountered: