-
Notifications
You must be signed in to change notification settings - Fork 12.8k
exactOptionalPropertyTypes being inconsistent with T[keyof T] #47587
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
Comments
const x: T = {};
const x2: T2 = x.a; would be a type error. |
I see your point. That's a pity, because it means there's no safe way to type an object's possible keys' possible values. |
Except it doesn't nullify the rule, because you can still read an |
This seems to be a misunderstanding of what EOPT means. The type you want instead of type NotUndefinedKey<T, K extends keyof T> = Pick<T, K> extends {} ? NotUndefined<T[K]> : T[K];
type GetWriteShape<T> = { [K in keyof T]-?: NotUndefinedKey<T, K> };
type TW = GetWriteShape<T>;
type M = TW[keyof TW]; |
@RyanCavanaugh This code correctly removes undefined from optional properties, but it removes it too, when undefined is allowed: type NotUndefined<T> = T extends undefined ? never : T;
type NotUndefinedKey<T, K extends keyof T> = Pick<T, K> extends {} ? NotUndefined<T[K]> : T[K];
type GetWriteShape<T> = { [K in keyof T]-?: NotUndefinedKey<T, K> };
type PossibleValues<T> = GetWriteShape<T>[keyof GetWriteShape<T>];
type T = { a: number, b: string, c: undefined }
type xxx = PossibleValues<T> // string | number
type yyy = T[keyof T] // string | number | undefined |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
Bug Report
π Search Terms
exact optional property types
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
T[keyof T]
isstring | number | undefined
π Expected behavior
T[keyof T]
isstring | number
The text was updated successfully, but these errors were encountered: