-
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
Avoid incorrectly reusing assertion nodes from property assignments #60576
Merged
jakebailey
merged 6 commits into
microsoft:main
from
Andarist:dont-reuse-assertion-nodes-with-different-types
Dec 4, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a2e2f05
Avoid incorrectly reusing assertion nodes from property assignments
Andarist a2db14d
Merge remote-tracking branch 'origin/main' into dont-reuse-assertion-…
Andarist 8282c6d
improve the fix
Andarist 1175802
remove obsolete import
Andarist f001c49
Merge remote-tracking branch 'origin/main' into dont-reuse-assertion-…
Andarist 0407bd6
add dts test
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.js
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 @@ | ||
//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] //// | ||
|
||
//// [declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] | ||
type Wrapper<T> = { | ||
_type: T; | ||
}; | ||
|
||
declare function stringWrapper(): Wrapper<string>; | ||
|
||
declare function objWrapper<T extends Record<string, Wrapper<any>>>( | ||
obj: T, | ||
): Wrapper<T>; | ||
|
||
const value = objWrapper({ | ||
prop1: stringWrapper() as Wrapper<"hello">, | ||
}); | ||
|
||
type Unwrap<T> = T extends Wrapper<any> | ||
? T["_type"] extends Record<string, Wrapper<any>> | ||
? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> } | ||
: T["_type"] | ||
: never; | ||
|
||
declare function unwrap<T>(wrapper: T): Unwrap<T>; | ||
|
||
export const unwrapped = unwrap(value); | ||
|
||
|
||
|
||
|
||
//// [declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.d.ts] | ||
export declare const unwrapped: { | ||
prop1: "hello"; | ||
}; | ||
Comment on lines
+31
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jakebailey here is the requested dts test |
78 changes: 78 additions & 0 deletions
78
tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.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,78 @@ | ||
//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] //// | ||
|
||
=== declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts === | ||
type Wrapper<T> = { | ||
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 13)) | ||
|
||
_type: T; | ||
>_type : Symbol(_type, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 19)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 13)) | ||
|
||
}; | ||
|
||
declare function stringWrapper(): Wrapper<string>; | ||
>stringWrapper : Symbol(stringWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 2, 2)) | ||
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) | ||
|
||
declare function objWrapper<T extends Record<string, Wrapper<any>>>( | ||
>objWrapper : Symbol(objWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 4, 50)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 28)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) | ||
|
||
obj: T, | ||
>obj : Symbol(obj, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 68)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 28)) | ||
|
||
): Wrapper<T>; | ||
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 28)) | ||
|
||
const value = objWrapper({ | ||
>value : Symbol(value, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 10, 5)) | ||
>objWrapper : Symbol(objWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 4, 50)) | ||
|
||
prop1: stringWrapper() as Wrapper<"hello">, | ||
>prop1 : Symbol(prop1, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 10, 26)) | ||
>stringWrapper : Symbol(stringWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 2, 2)) | ||
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) | ||
|
||
}); | ||
|
||
type Unwrap<T> = T extends Wrapper<any> | ||
>Unwrap : Symbol(Unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 12, 3)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) | ||
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) | ||
|
||
? T["_type"] extends Record<string, Wrapper<any>> | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) | ||
|
||
? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> } | ||
>Key : Symbol(Key, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 16, 9)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) | ||
>Unwrap : Symbol(Unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 12, 3)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) | ||
>Key : Symbol(Key, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 16, 9)) | ||
|
||
: T["_type"] | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) | ||
|
||
: never; | ||
|
||
declare function unwrap<T>(wrapper: T): Unwrap<T>; | ||
>unwrap : Symbol(unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 18, 10)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 24)) | ||
>wrapper : Symbol(wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 27)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 24)) | ||
>Unwrap : Symbol(Unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 12, 3)) | ||
>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 24)) | ||
|
||
export const unwrapped = unwrap(value); | ||
>unwrapped : Symbol(unwrapped, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 22, 12)) | ||
>unwrap : Symbol(unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 18, 10)) | ||
>value : Symbol(value, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 10, 5)) | ||
|
74 changes: 74 additions & 0 deletions
74
tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.types
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,74 @@ | ||
//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] //// | ||
|
||
=== declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts === | ||
type Wrapper<T> = { | ||
>Wrapper : Wrapper<T> | ||
> : ^^^^^^^^^^ | ||
|
||
_type: T; | ||
>_type : T | ||
> : ^ | ||
|
||
}; | ||
|
||
declare function stringWrapper(): Wrapper<string>; | ||
>stringWrapper : () => Wrapper<string> | ||
> : ^^^^^^ | ||
|
||
declare function objWrapper<T extends Record<string, Wrapper<any>>>( | ||
>objWrapper : <T extends Record<string, Wrapper<any>>>(obj: T) => Wrapper<T> | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
|
||
obj: T, | ||
>obj : T | ||
> : ^ | ||
|
||
): Wrapper<T>; | ||
|
||
const value = objWrapper({ | ||
>value : Wrapper<{ prop1: Wrapper<"hello">; }> | ||
> : ^^^^^^^^^^^^^^^^^ ^^^^ | ||
>objWrapper({ prop1: stringWrapper() as Wrapper<"hello">,}) : Wrapper<{ prop1: Wrapper<"hello">; }> | ||
> : ^^^^^^^^^^^^^^^^^ ^^^^ | ||
>objWrapper : <T extends Record<string, Wrapper<any>>>(obj: T) => Wrapper<T> | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
>{ prop1: stringWrapper() as Wrapper<"hello">,} : { prop1: Wrapper<"hello">; } | ||
> : ^^^^^^^^^ ^^^ | ||
|
||
prop1: stringWrapper() as Wrapper<"hello">, | ||
>prop1 : Wrapper<"hello"> | ||
> : ^^^^^^^^^^^^^^^^ | ||
>stringWrapper() as Wrapper<"hello"> : Wrapper<"hello"> | ||
> : ^^^^^^^^^^^^^^^^ | ||
>stringWrapper() : Wrapper<string> | ||
> : ^^^^^^^^^^^^^^^ | ||
>stringWrapper : () => Wrapper<string> | ||
> : ^^^^^^ | ||
|
||
}); | ||
|
||
type Unwrap<T> = T extends Wrapper<any> | ||
>Unwrap : Unwrap<T> | ||
> : ^^^^^^^^^ | ||
|
||
? T["_type"] extends Record<string, Wrapper<any>> | ||
? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> } | ||
: T["_type"] | ||
: never; | ||
|
||
declare function unwrap<T>(wrapper: T): Unwrap<T>; | ||
>unwrap : <T>(wrapper: T) => Unwrap<T> | ||
> : ^ ^^ ^^ ^^^^^ | ||
>wrapper : T | ||
> : ^ | ||
|
||
export const unwrapped = unwrap(value); | ||
>unwrapped : { prop1: "hello"; } | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
>unwrap(value) : { prop1: "hello"; } | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
>unwrap : <T>(wrapper: T) => Unwrap<T> | ||
> : ^ ^^ ^^ ^^^^^ | ||
>value : Wrapper<{ prop1: Wrapper<"hello">; }> | ||
> : ^^^^^^^^^^^^^^^^^ ^^^^ | ||
|
27 changes: 27 additions & 0 deletions
27
tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts
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 @@ | ||
// @strict: true | ||
// @declaration: true | ||
// @emitDeclarationOnly: true | ||
|
||
type Wrapper<T> = { | ||
_type: T; | ||
}; | ||
|
||
declare function stringWrapper(): Wrapper<string>; | ||
|
||
declare function objWrapper<T extends Record<string, Wrapper<any>>>( | ||
obj: T, | ||
): Wrapper<T>; | ||
|
||
const value = objWrapper({ | ||
prop1: stringWrapper() as Wrapper<"hello">, | ||
}); | ||
|
||
type Unwrap<T> = T extends Wrapper<any> | ||
? T["_type"] extends Record<string, Wrapper<any>> | ||
? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> } | ||
: T["_type"] | ||
: never; | ||
|
||
declare function unwrap<T>(wrapper: T): Unwrap<T>; | ||
|
||
export const unwrapped = unwrap(value); |
36 changes: 36 additions & 0 deletions
36
tests/cases/fourslash/quickInfoAssertionNodeNotReusedWhenTypeNotEquivalent1.ts
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,36 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// https://github.com/microsoft/TypeScript/issues/60573 | ||
|
||
// @strict: true | ||
|
||
//// type Wrapper<T> = { | ||
//// _type: T; | ||
//// }; | ||
//// | ||
//// function stringWrapper(): Wrapper<string> { | ||
//// return { _type: "" }; | ||
//// } | ||
//// | ||
//// function objWrapper<T extends Record<string, Wrapper<any>>>( | ||
//// obj: T, | ||
//// ): Wrapper<T> { | ||
//// return { _type: obj }; | ||
//// } | ||
//// | ||
//// const value = objWrapper({ | ||
//// prop1: stringWrapper() as Wrapper<"hello">, | ||
//// }); | ||
//// | ||
//// type Unwrap<T extends Wrapper<any>> = T["_type"] extends Record< | ||
//// string, | ||
//// Wrapper<any> | ||
//// > | ||
//// ? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> } | ||
//// : T["_type"]; | ||
//// | ||
//// type Test/*1*/ = Unwrap<typeof value>; | ||
|
||
verify.quickInfoAt("1", `type Test = { | ||
prop1: "hello"; | ||
}`) |
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.
I was largely unsure about this given you ask a question about
node
then use that to work withassertionNode
, but this is effectively the same check as:So I definitely like this change more than the initial version.