-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement(60881): Added detailed diagnostic for parent to child assignment #61145
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
Changes from 31 commits
a010044
3d632b3
ef0dd9d
e16f35f
63e6b4c
09022f3
8def683
ce15fd8
01e2bfc
85dc004
47acf44
65fdfc1
4f8eeb6
4db251b
6505632
c0b8e55
d62eed1
753d8c1
694b893
ef4e281
dd6ab9a
2a56797
a697946
c662343
cad1a24
b3664f8
b285ebc
95b249f
c88940c
8933230
eab824d
c741d1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| better_subType_assignable_to_superType_error_messsage.ts(2,3): error TS2552: Cannot find name 'subtype'. Did you mean 'subType2'? | ||
| better_subType_assignable_to_superType_error_messsage.ts(5,3): error TS2322: Type 'SuperType' is not assignable to type 'SubType2'. | ||
| 'SubType2' is constrained to be a subtype of 'SuperType'. | ||
|
|
||
|
|
||
| ==== better_subType_assignable_to_superType_error_messsage.ts (2 errors) ==== | ||
| function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) { | ||
| subtype = superType;//error | ||
| ~~~~~~~ | ||
| !!! error TS2552: Cannot find name 'subtype'. Did you mean 'subType2'? | ||
| !!! related TS2728 better_subType_assignable_to_superType_error_messsage.ts:1:127: 'subType2' is declared here. | ||
|
|
||
| //ensures that supertypes are not assignable to 'grandchild' subtypes | ||
| subType2 = superType;//error | ||
| ~~~~~~~~ | ||
| !!! error TS2322: Type 'SuperType' is not assignable to type 'SubType2'. | ||
| !!! error TS2322: 'SubType2' is constrained to be a subtype of 'SuperType'. | ||
| !!! related TS2208 better_subType_assignable_to_superType_error_messsage.ts:1:41: This type parameter might need an `extends SubType2` constraint. | ||
|
|
||
| superType = subType2;//ok | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] //// | ||
|
|
||
| //// [better_subType_assignable_to_superType_error_messsage.ts] | ||
| function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) { | ||
| subtype = superType;//error | ||
|
|
||
| //ensures that supertypes are not assignable to 'grandchild' subtypes | ||
| subType2 = superType;//error | ||
|
|
||
| superType = subType2;//ok | ||
| } | ||
|
|
||
|
|
||
| //// [better_subType_assignable_to_superType_error_messsage.js] | ||
| function parameterExtendsOtherParameter(superType, subType2) { | ||
| subtype = superType; //error | ||
| //ensures that supertypes are not assignable to 'grandchild' subtypes | ||
| subType2 = superType; //error | ||
| superType = subType2; //ok | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] //// | ||
|
|
||
| === better_subType_assignable_to_superType_error_messsage.ts === | ||
| function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) { | ||
| >parameterExtendsOtherParameter : Symbol(parameterExtendsOtherParameter, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 0)) | ||
| >SuperType : Symbol(SuperType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 40)) | ||
| >SubType : Symbol(SubType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 50)) | ||
| >SuperType : Symbol(SuperType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 40)) | ||
| >SubType2 : Symbol(SubType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 77)) | ||
| >SubType : Symbol(SubType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 50)) | ||
| >superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) | ||
| >SuperType : Symbol(SuperType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 40)) | ||
| >subType2 : Symbol(subType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 125)) | ||
| >SubType2 : Symbol(SubType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 77)) | ||
|
|
||
| subtype = superType;//error | ||
| >superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) | ||
|
|
||
| //ensures that supertypes are not assignable to 'grandchild' subtypes | ||
| subType2 = superType;//error | ||
| >subType2 : Symbol(subType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 125)) | ||
| >superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) | ||
|
|
||
| superType = subType2;//ok | ||
| >superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) | ||
| >subType2 : Symbol(subType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 125)) | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| //// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] //// | ||
|
|
||
| === better_subType_assignable_to_superType_error_messsage.ts === | ||
| function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) { | ||
| >parameterExtendsOtherParameter : <SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) => void | ||
| > : ^ ^^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ | ||
| >superType : SuperType | ||
| > : ^^^^^^^^^ | ||
| >subType2 : SubType2 | ||
| > : ^^^^^^^^ | ||
|
|
||
| subtype = superType;//error | ||
| >subtype = superType : SuperType | ||
| > : ^^^^^^^^^ | ||
| >subtype : any | ||
| > : ^^^ | ||
| >superType : SuperType | ||
| > : ^^^^^^^^^ | ||
|
|
||
| //ensures that supertypes are not assignable to 'grandchild' subtypes | ||
| subType2 = superType;//error | ||
| >subType2 = superType : SuperType | ||
| > : ^^^^^^^^^ | ||
| >subType2 : SubType2 | ||
| > : ^^^^^^^^ | ||
| >superType : SuperType | ||
| > : ^^^^^^^^^ | ||
|
|
||
| superType = subType2;//ok | ||
| >superType = subType2 : SubType2 | ||
| > : ^^^^^^^^ | ||
| >superType : SuperType | ||
| > : ^^^^^^^^^ | ||
| >subType2 : SubType2 | ||
| > : ^^^^^^^^ | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| conditionalTypes2.ts(15,5): error TS2322: Type 'Covariant<A>' is not assignable to type 'Covariant<B>'. | ||
| Type 'A' is not assignable to type 'B'. | ||
| 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. | ||
| 'B' is constrained to be a subtype of 'A'. | ||
|
Member
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. I'm not understanding why this error message is an improvement. What's the smallest example where this error is going to be easier to understand? 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. @RyanCavanaugh This error message is an improvement because the original error message says that the two types could be unrelated but we know they are related in a parent child relationship, you just can't assign a parent to a child. We tried to pick a new error message that was similar to the suggestion in the original issue. This is the simplest example we looked at: |
||
| conditionalTypes2.ts(19,5): error TS2322: Type 'Contravariant<B>' is not assignable to type 'Contravariant<A>'. | ||
| Type 'A' is not assignable to type 'B'. | ||
| 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. | ||
| 'B' is constrained to be a subtype of 'A'. | ||
| conditionalTypes2.ts(24,5): error TS2322: Type 'Invariant<B>' is not assignable to type 'Invariant<A>'. | ||
| Types of property 'foo' are incompatible. | ||
| Type 'B extends string ? keyof B : B' is not assignable to type 'A extends string ? keyof A : A'. | ||
|
|
@@ -23,7 +23,7 @@ conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant<A>' is not assignable | |
| Type 'A | keyof A' is not assignable to type 'B extends string ? keyof B : B'. | ||
| Type 'A' is not assignable to type 'B extends string ? keyof B : B'. | ||
| Type 'A' is not assignable to type 'B'. | ||
| 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. | ||
| 'B' is constrained to be a subtype of 'A'. | ||
| conditionalTypes2.ts(73,12): error TS2345: Argument of type 'Extract<Extract<T, Foo>, Bar>' is not assignable to parameter of type '{ foo: string; bat: string; }'. | ||
| Type 'Extract<T, Bar>' is not assignable to type '{ foo: string; bat: string; }'. | ||
| Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'. | ||
|
|
@@ -53,7 +53,7 @@ conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2<T, Foo, Ba | |
| ~ | ||
| !!! error TS2322: Type 'Covariant<A>' is not assignable to type 'Covariant<B>'. | ||
| !!! error TS2322: Type 'A' is not assignable to type 'B'. | ||
| !!! error TS2322: 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. | ||
| !!! error TS2322: 'B' is constrained to be a subtype of 'A'. | ||
| !!! related TS2208 conditionalTypes2.ts:13:13: This type parameter might need an `extends B` constraint. | ||
| } | ||
|
|
||
|
|
@@ -62,7 +62,7 @@ conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2<T, Foo, Ba | |
| ~ | ||
| !!! error TS2322: Type 'Contravariant<B>' is not assignable to type 'Contravariant<A>'. | ||
| !!! error TS2322: Type 'A' is not assignable to type 'B'. | ||
| !!! error TS2322: 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. | ||
| !!! error TS2322: 'B' is constrained to be a subtype of 'A'. | ||
| !!! related TS2208 conditionalTypes2.ts:18:13: This type parameter might need an `extends B` constraint. | ||
| b = a; | ||
| } | ||
|
|
@@ -91,7 +91,7 @@ conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2<T, Foo, Ba | |
| !!! error TS2322: Type 'A | keyof A' is not assignable to type 'B extends string ? keyof B : B'. | ||
| !!! error TS2322: Type 'A' is not assignable to type 'B extends string ? keyof B : B'. | ||
| !!! error TS2322: Type 'A' is not assignable to type 'B'. | ||
| !!! error TS2322: 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. | ||
| !!! error TS2322: 'B' is constrained to be a subtype of 'A'. | ||
| !!! related TS2208 conditionalTypes2.ts:23:13: This type parameter might need an `extends B` constraint. | ||
| !!! related TS2208 conditionalTypes2.ts:23:13: This type parameter might need an `extends B extends string ? keyof B : B` constraint. | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.