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
Maintain modifiers on Omit
#31205
Merged
Merged
Maintain modifiers on Omit
#31205
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/baselines/reference/omitTypeHelperModifiers01.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
tests/cases/compiler/omitTypeHelperModifiers01.ts(16,7): error TS2540: Cannot assign to 'c' because it is a read-only property. | ||
|
||
|
||
==== tests/cases/compiler/omitTypeHelperModifiers01.ts (1 errors) ==== | ||
type A = { | ||
a: number; | ||
b?: string; | ||
readonly c: boolean; | ||
d: unknown; | ||
}; | ||
|
||
type B = Omit<A, 'a'>; | ||
|
||
function f(x: B) { | ||
const b = x.b; | ||
x.b = "hello"; | ||
x.b = undefined; | ||
|
||
const c = x.c; | ||
x.c = true; | ||
~ | ||
!!! error TS2540: Cannot assign to 'c' because it is a read-only property. | ||
|
||
const d = x.d; | ||
x.d = d; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//// [omitTypeHelperModifiers01.ts] | ||
type A = { | ||
a: number; | ||
b?: string; | ||
readonly c: boolean; | ||
d: unknown; | ||
}; | ||
|
||
type B = Omit<A, 'a'>; | ||
|
||
function f(x: B) { | ||
const b = x.b; | ||
x.b = "hello"; | ||
x.b = undefined; | ||
|
||
const c = x.c; | ||
x.c = true; | ||
|
||
const d = x.d; | ||
x.d = d; | ||
} | ||
|
||
|
||
//// [omitTypeHelperModifiers01.js] | ||
"use strict"; | ||
function f(x) { | ||
var b = x.b; | ||
x.b = "hello"; | ||
x.b = undefined; | ||
var c = x.c; | ||
x.c = true; | ||
var d = x.d; | ||
x.d = d; | ||
} |
69 changes: 69 additions & 0 deletions
69
tests/baselines/reference/omitTypeHelperModifiers01.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
=== tests/cases/compiler/omitTypeHelperModifiers01.ts === | ||
type A = { | ||
>A : Symbol(A, Decl(omitTypeHelperModifiers01.ts, 0, 0)) | ||
|
||
a: number; | ||
>a : Symbol(a, Decl(omitTypeHelperModifiers01.ts, 0, 10)) | ||
|
||
b?: string; | ||
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14)) | ||
|
||
readonly c: boolean; | ||
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15)) | ||
|
||
d: unknown; | ||
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24)) | ||
|
||
}; | ||
|
||
type B = Omit<A, 'a'>; | ||
>B : Symbol(B, Decl(omitTypeHelperModifiers01.ts, 5, 2)) | ||
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --)) | ||
>A : Symbol(A, Decl(omitTypeHelperModifiers01.ts, 0, 0)) | ||
|
||
function f(x: B) { | ||
>f : Symbol(f, Decl(omitTypeHelperModifiers01.ts, 7, 22)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>B : Symbol(B, Decl(omitTypeHelperModifiers01.ts, 5, 2)) | ||
|
||
const b = x.b; | ||
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 10, 9)) | ||
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14)) | ||
|
||
x.b = "hello"; | ||
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14)) | ||
|
||
x.b = undefined; | ||
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14)) | ||
>undefined : Symbol(undefined) | ||
|
||
const c = x.c; | ||
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 14, 9)) | ||
>x.c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15)) | ||
|
||
x.c = true; | ||
>x.c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15)) | ||
|
||
const d = x.d; | ||
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 17, 9)) | ||
>x.d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24)) | ||
|
||
x.d = d; | ||
>x.d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24)) | ||
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11)) | ||
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24)) | ||
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 17, 9)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
=== tests/cases/compiler/omitTypeHelperModifiers01.ts === | ||
type A = { | ||
>A : A | ||
|
||
a: number; | ||
>a : number | ||
|
||
b?: string; | ||
>b : string | undefined | ||
|
||
readonly c: boolean; | ||
>c : boolean | ||
|
||
d: unknown; | ||
>d : unknown | ||
|
||
}; | ||
|
||
type B = Omit<A, 'a'>; | ||
>B : Pick<A, "b" | "c" | "d"> | ||
|
||
function f(x: B) { | ||
>f : (x: Pick<A, "b" | "c" | "d">) => void | ||
>x : Pick<A, "b" | "c" | "d"> | ||
|
||
const b = x.b; | ||
>b : string | undefined | ||
>x.b : string | undefined | ||
>x : Pick<A, "b" | "c" | "d"> | ||
>b : string | undefined | ||
|
||
x.b = "hello"; | ||
>x.b = "hello" : "hello" | ||
>x.b : string | undefined | ||
>x : Pick<A, "b" | "c" | "d"> | ||
>b : string | undefined | ||
>"hello" : "hello" | ||
|
||
x.b = undefined; | ||
>x.b = undefined : undefined | ||
>x.b : string | undefined | ||
>x : Pick<A, "b" | "c" | "d"> | ||
>b : string | undefined | ||
>undefined : undefined | ||
|
||
const c = x.c; | ||
>c : boolean | ||
>x.c : boolean | ||
>x : Pick<A, "b" | "c" | "d"> | ||
>c : boolean | ||
|
||
x.c = true; | ||
>x.c = true : true | ||
>x.c : any | ||
>x : Pick<A, "b" | "c" | "d"> | ||
>c : any | ||
>true : true | ||
|
||
const d = x.d; | ||
>d : unknown | ||
>x.d : unknown | ||
>x : Pick<A, "b" | "c" | "d"> | ||
>d : unknown | ||
|
||
x.d = d; | ||
>x.d = d : unknown | ||
>x.d : unknown | ||
>x : Pick<A, "b" | "c" | "d"> | ||
>d : unknown | ||
>d : unknown | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🤔Why can't this be
K extends keyof T
to make the type homomorphic?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.
I don't believe that would make the type homomorphic, would it @ahejlsberg?
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.
Yeah, discussed offline, but for reasons outlined below, the fact that it's not just a simple
keyof T
orK
(whereK extends keyof T
) means that TypeScript can't trivially look at that and say "yup, we gotta keep the modifiers around on this one".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.
Could also do
that is to say: making the builtin
Omit
distributive. (Which'd give it an alias symbol, too) A bunch of library authors end up swapping their internalOmit
to this once they get into situations involving unions.Distributive conditional like this have perf issues when nested repeatedly right now, tho, sooooo.... ehhhhh?