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
Fix Diff and Omit #21156
Fix Diff and Omit #21156
Conversation
src/compiler/checker.ts
Outdated
} | ||
|
||
function getSubstitutedIndexedMappedType(type: IndexedAccessType): Type { | ||
const objectType = type.objectType; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { objectType } = type
?
//// [indexedAccessRetainsIndexSignature.ts] | ||
type Diff<T extends string, U extends string> = | ||
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] | ||
type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(on many people's wishlist): add these to lib.d.ts
💟
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diff and Omit might someday be in lib.d.ts once conditional types are in Typescript, but the current implementation is way too hacky.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, only Diff implementation is way too hacky. But it will be solved if subtraction types make it into typescript (someday).
type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>> | ||
|
||
|
||
type O = Omit<{ a: number, b: string }, 'a'> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better add this to ensure a type error shows up if this breaks
const o: O = { b: '' }
Does it also repair 2.6.2 incorrect behaviour aroud type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>; is in fact an equivalent of type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; and since now the latter works incorrectly, both of them should be incorporated in tests and they should also check if they both maintain optional and readonly fields (refer to #12215 (comment) ). |
@aczekajski This fixes the incorrect error message caused by #17912. It does not fix the weird behaviour with optional properties. |
when transforming an indexed access type in order to get its constraint.
Looks like one of the Travis instances timed out. I'm going to merge this after getting verbal signoff from Mohamed and Anders. |
Fixes #21148
This adds another transformation for indexed access types when computing their constraint. The new transformation removes any mapped type whose template type is never when the object type is an intersection. For example,
This works because this mapped type will only ever remove properties that would otherwise be part of the resulting indexed access type. Specifically, it is useful for
Diff
:This further simplifies to
After further simplifications.
This change is important because the constraint of indexed access type is used for assignability, and before #17912 the constraint of indexed access types like this was mistakenly
any
. Now that the constraint is a real type, it's important that it's the right one.Notes:
Diff
andOmit
to our tests.