We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
in
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
TypeScript Version: 3.5.2
Search Terms: Omit, guard
Code
type t1 = { a: boolean } & ({ x: number } | { y: string } | {}) const z1: t1 = Date.now() === 123453 ? { a: true, x: 1 } : { a: true } const z2: Omit<t1, 'a'> = z1 console.log('x' in z2 && z2.x)
Expected behavior: no errors Actual behavior:
Error:(235, 29) TS2339: Property 'x' does not exist on type 'never'.
Playground Link: Link to Playground
If I specify the actual type instead of using Omit, everything is working with no errors:
type t1 = { a: boolean } & ({ x: number } | { y: string } | {}) const z1: t1 = Date.now() === 123453 ? { a: true, x: 1 } : { a: true } const z2: { x: number } | { y: string } | {} = z1 console.log('x' in z2 && z2.x)
The text was updated successfully, but these errors were encountered:
For this to work you need to implement your mapped types in a distributive way. See #28339 for extensive discussion.
TLDR:
// Omit copied from typescript lib.es5.d.ts file type UnionKeys<T> = T extends any ? keyof T : never type DistributivePick<T, K extends UnionKeys<T>> = T extends any ? Pick<T, Extract<keyof T, K>> : never; type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> type DistributiveOmit<T, K extends UnionKeys<T>> = T extends any ? Omit<T, Extract<keyof T, K>> : never; type t1 = { a: boolean } & ({ x: number } | { y: string } | {}) const z1: t1 = Date.now() === 123453 ? { a: true, x: 1 } : { a: true } const z2: DistributiveOmit<t1, 'a'> = z1 console.log('x' in z2 && z2.x)
Sorry, something went wrong.
Okay. You gave me a lot to learn before I can respond.
If this is working as Intended, close this issue.
No branches or pull requests
TypeScript Version: 3.5.2
Search Terms: Omit, guard
Code
Expected behavior:
no errors
Actual behavior:
Playground Link:
Link to Playground
If I specify the actual type instead of using Omit, everything is working with no errors:
The text was updated successfully, but these errors were encountered: