Skip to content

use default type parameter rather than unknown when show quick help #46792

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 2 commits into from
Dec 9, 2021

Conversation

ShuiRuTian
Copy link
Contributor

Fixes #46774

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Nov 12, 2021
while (typeArguments.length > typeParameters.length) {
typeArguments.pop();
}
typeArguments = fillMissingTypeArguments(typeArguments, typeParameters, getMinTypeArgumentCount(typeParameters), isJs);
while (typeArguments.length < typeParameters.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases does this loop still run?

Copy link
Contributor Author

@ShuiRuTian ShuiRuTian Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this(the example is from instantiateGenericClassWithWrongNumberOfTypeArguments.ts):

class D<T, U> {
    x: T
    y: U
}

var d = new D<number>();
// when hover on `d`, it shows `var d: D<number, unknown>`
//                                               ^^^^^^ here is added by this loop, and if `U` has a constraint, it would be the constraint.

fillMissingTypeArguments only fills typeArgument when there are valid default type parameter, which means the number of typeArgument is between minTypeParameterCount and typeParameterCount. It does not always fill the type argument to full.

Copy link
Contributor Author

@ShuiRuTian ShuiRuTian Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, we would better to mention this case:

interface A { a: number }
interface B extends A { b: string }

class D<T, U, V extends A = B> {
    x: T
    y: U
    z: V
}

var d = new D<number>();
// when hover on `d`, it shows `var d: D<number, unknown, A>`
// `A` is its constraint rather than the default type parameter `B`, this PR does not change this behavior now

However, it is a little different: there is an error on the number of type arguments. So I feel that both methods are OK? Or the default type parameter is better than constraint?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I feel like using the default here makes a little more sense, but I don’t know that it’s super critical since it’s an error anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now prefer default type parameter than constraint for quick info. Correspond tests are added.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted that this change doesn’t just affect quick info; it actually affects the type instantiated in these kinds of error scenarios. I’m surprised there weren’t any existing baselines that needed updating. In your example above, you can check the type of d.z and see that in main, the type is A, and this branch, the type is B.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new behavior is more intuitive to me, but others should weigh in. The most conservative thing would be to leave it as it was, against my earlier advice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out, I did not notice it! This does make sense. And, I am surprised too :)
But do we really need to revert to last commit? For:

  1. we are doing things in the right way
  2. anyway, it is just an error condition which finally need be fixed by user

Again, I feel both methods are OK. The final decision is up to you~

@andrewbranch
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2021

Heya @andrewbranch, I've started to run the tarball bundle task on this PR at a0aa803. You can monitor the build here.

@andrewbranch
Copy link
Member

npm died 😦

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2021

Heya @andrewbranch, I've started to run the tarball bundle task on this PR at a0aa803. You can monitor the build here.

@andrewbranch
Copy link
Member

@typescript-bot pack this please

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2021

Heya @andrewbranch, I've started to run the tarball bundle task on this PR at a0aa803. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2021

Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/115173/artifacts?artifactName=tgz&fileId=27B511EEB46E38603FFE7E443A46392F30DBBDE2CAAFDB59058D0EFF3B9B5B4402&fileName=/typescript-4.6.0-insiders.20211116.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

@sandersn sandersn merged commit e2c89af into microsoft:main Dec 9, 2021
mprobst pushed a commit to mprobst/TypeScript that referenced this pull request Jan 10, 2022
…microsoft#46792)

* use default type parameter rather than `unknown` when show quick help

* prefer default type parameter than constraint for quick info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Quick info shows wrong type for type parameters instantiated with their default
5 participants