-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add missing type argument constraints check #51766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/unmetTypeConstraintInImportCall.errors.txt
This file contains hidden or 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,12 @@ | ||
tests/cases/compiler/file2.ts(1,37): error TS2344: Type 'T' does not satisfy the constraint 'string'. | ||
|
||
|
||
==== tests/cases/compiler/file1.ts (0 errors) ==== | ||
export type Foo<T extends string> = { foo: T } | ||
|
||
==== tests/cases/compiler/file2.ts (1 errors) ==== | ||
type Bar<T> = import('./file1').Foo<T>; | ||
~ | ||
!!! error TS2344: Type 'T' does not satisfy the constraint 'string'. | ||
!!! related TS2208 tests/cases/compiler/file2.ts:1:10: This type parameter might need an `extends string` constraint. | ||
|
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/unmetTypeConstraintInImportCall.symbols
This file contains hidden or 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,14 @@ | ||
=== tests/cases/compiler/file1.ts === | ||
export type Foo<T extends string> = { foo: T } | ||
>Foo : Symbol(Foo, Decl(file1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(file1.ts, 0, 16)) | ||
>foo : Symbol(foo, Decl(file1.ts, 0, 37)) | ||
>T : Symbol(T, Decl(file1.ts, 0, 16)) | ||
|
||
=== tests/cases/compiler/file2.ts === | ||
type Bar<T> = import('./file1').Foo<T>; | ||
>Bar : Symbol(Bar, Decl(file2.ts, 0, 0)) | ||
>T : Symbol(T, Decl(file2.ts, 0, 9)) | ||
>Foo : Symbol(Foo, Decl(file1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(file2.ts, 0, 9)) | ||
|
9 changes: 9 additions & 0 deletions
9
tests/baselines/reference/unmetTypeConstraintInImportCall.types
This file contains hidden or 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,9 @@ | ||
=== tests/cases/compiler/file1.ts === | ||
export type Foo<T extends string> = { foo: T } | ||
>Foo : Foo<T> | ||
>foo : T | ||
|
||
=== tests/cases/compiler/file2.ts === | ||
type Bar<T> = import('./file1').Foo<T>; | ||
>Bar : Bar<T> | ||
|
20 changes: 20 additions & 0 deletions
20
tests/baselines/reference/unmetTypeConstraintInJSDocImportCall.errors.txt
This file contains hidden or 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,20 @@ | ||
tests/cases/compiler/file2.js(3,36): error TS2344: Type 'T' does not satisfy the constraint 'string'. | ||
|
||
|
||
==== tests/cases/compiler/file1.js (0 errors) ==== | ||
/** | ||
* @template {string} T | ||
* @typedef {{ foo: T }} Foo | ||
*/ | ||
|
||
export default {}; | ||
|
||
==== tests/cases/compiler/file2.js (1 errors) ==== | ||
/** | ||
* @template T | ||
* @typedef {import('./file1').Foo<T>} Bar | ||
~ | ||
!!! error TS2344: Type 'T' does not satisfy the constraint 'string'. | ||
!!! related TS2208 tests/cases/compiler/file2.js:2:14: This type parameter might need an `extends string` constraint. | ||
*/ | ||
|
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/unmetTypeConstraintInJSDocImportCall.symbols
This file contains hidden or 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,16 @@ | ||
=== tests/cases/compiler/file1.js === | ||
|
||
/** | ||
* @template {string} T | ||
* @typedef {{ foo: T }} Foo | ||
*/ | ||
|
||
export default {}; | ||
|
||
=== tests/cases/compiler/file2.js === | ||
|
||
/** | ||
* @template T | ||
* @typedef {import('./file1').Foo<T>} Bar | ||
*/ | ||
|
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/unmetTypeConstraintInJSDocImportCall.types
This file contains hidden or 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,16 @@ | ||
=== tests/cases/compiler/file1.js === | ||
/** | ||
* @template {string} T | ||
* @typedef {{ foo: T }} Foo | ||
*/ | ||
|
||
export default {}; | ||
>{} : {} | ||
|
||
=== tests/cases/compiler/file2.js === | ||
|
||
/** | ||
* @template T | ||
* @typedef {import('./file1').Foo<T>} Bar | ||
*/ | ||
|
This file contains hidden or 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,7 @@ | ||
// @noEmit: true | ||
// @filename: file1.ts | ||
export type Foo<T extends string> = { foo: T } | ||
|
||
// @noEmit: true | ||
// @filename: file2.ts | ||
type Bar<T> = import('./file1').Foo<T>; |
19 changes: 19 additions & 0 deletions
19
tests/cases/compiler/unmetTypeConstraintInJSDocImportCall.ts
This file contains hidden or 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,19 @@ | ||
// @allowJs: true | ||
// @checkJs: true | ||
// @noEmit: true | ||
// @filename: file1.js | ||
/** | ||
* @template {string} T | ||
* @typedef {{ foo: T }} Foo | ||
*/ | ||
|
||
export default {}; | ||
|
||
// @allowJs: true | ||
// @checkJs: true | ||
// @noEmit: true | ||
// @filename: file2.js | ||
/** | ||
* @template T | ||
* @typedef {import('./file1').Foo<T>} Bar | ||
*/ |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
is there a reason you can't inspect the type returned from
tryGetDeclaredTypeOfSymbol
for type parameters? Or areTypeParameter
s only available only on Symbol(Links) instead of type references?tryGetDeclaredTypeOfSymbol(resolvedSymbol).typeParameters
would work, I think, if you check that the type is an InterfaceType (getObjectFlags(t) & (ObjectFlags.Class | ObjectFlags.Interface)
) and that typeParameters is notundefined
.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.
@sandersn
So I tried the approach that you suggested and I observed that the
type
returned bytryGetDeclaredTypeOfSymbol()
is notInterfaceType
butTypeReference
, wheretarget
isInterfaceType
. When I trytarget.typeParameters
, this unfortunately doesn't work because the elements oftarget.typeParameters
do not haveconstraint
property for some reason.Next, I examined other uses of
checkTypeArgumentConstraint()
and it seems liketypeParameters
are obtained viagetTypeParametersForTypeReference()
. When you check the implementation of that function, it seems to be conceptually equivalent to what I was doing (it usesgetSymbolLinks()
at the end of the day):So I ended up extracting a
getTypeParametersForTypeAndSymbol()
sub-routine in order to leverage the same logic for handling edge cases. Does that make more sense to you now?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.
Yes, extracting the existing code seems like a good solution.