Skip to content

suggest fix when using value as type argument - #32543

Closed
John Patterson (john-patterson) wants to merge 9 commits into
microsoft:masterfrom
john-patterson:value-as-type-error
Closed

John Patterson (john-patterson) wants to merge 9 commits into
microsoft:masterfrom
john-patterson:value-as-type-error

Conversation

@john-patterson

Copy link
Copy Markdown
Contributor

Fixes #28975.

As discussed in the body of the issue, this only makes a new suggestion when a value appears with as a type argument, like .

So, given the setup from #28975:
type Foo<T> = {}; const Something = [];,

This generates the new "error TS2774: 'Something' refers to a value, but is being used as a type argument here. Did you mean 'typeof Something'?" error:

type SomeFoo = Foo<Something>;

but a simple aliasing,

type SomeBar = Something;

generates the old "error TS2749: 'Something' refers to a value, but is being used as a type here." error.

I couldn't figure out a cleaner way to write the isIdentifierATypeArgument function. Currently, I'm checking the failing node's grandparent to see if the current node is in that node's typeArguments. This involves some casting and a findIndex, so if there's a better way to divine that with either meaning or node.kind, that would be preferrable.

@john-patterson John Patterson (john-patterson) changed the title Value as type error suggest fix when using value as type argument Jul 24, 2019
@RyanCavanaugh

Copy link
Copy Markdown
Member

So the original issue outline in the issue showed this:

type Foo<T> = {}

const Something = []

type FooSomething = Foo<Something>
//                      ^
//                      Can not find name "Something"   

The error has since been updated to

//                      ^
//                      "Something" refers to a value, but is being used as a type here

The validity of suggesting to use typeof doesn't seem at all specific to a type argument position. The error message can just universally offer that as a potential idea and the developer can decide whether or not that's what they meant. IMO this should just be an update to the error message text without any conditionalizing based on the position of the identifier.

Wesley Wigham (@weswigham) What were your concrete concerns here? It wasn't clear from Daniel's comments

@weswigham

Copy link
Copy Markdown
Member

Ryan Cavanaugh (@RyanCavanaugh) just that suggesting typeof X when X refers to a function value isn't likely correct - it's more often you mean the return type of X, especially if X is being used in a class-like way.

@john-patterson

Copy link
Copy Markdown
Contributor Author

Wesley Wigham (@weswigham) I'm still a little unsure of what you mean.

What I'm understanding as your argument is:

class Foo {}
function Bar(): Foo { return {}; }
type Baz = Bar;

it's more likely a user wants Baz to be equivalent to Foo instead of typeof Bar?

@john-patterson

Copy link
Copy Markdown
Contributor Author

As a side-note, it might be useful (on this PR or some other) to add an actual code fix operation for to go along with this error message change. Any objections?

@weswigham

Copy link
Copy Markdown
Member

John Patterson (@john-patterson) yep, that was my premise. Its probably not too important, it was just a scenario to think about while this was implemented.

@john-patterson

John Patterson (john-patterson) commented Oct 10, 2019 •

Copy link
Copy Markdown
Contributor Author

Sorry to have orphaned this PR. To be clear, the issue is now just changing this message:

"Something" refers to a value, but is being used as a type here

To be something like:

"Something" refers to a value, but is being used as a type here. Did you mean 'typeof Something'?

I'll try to remember to gut the rest of this PR this weekend.

This addresses issue microsoft#28975
(microsoft#28975).

When providing a value as a type argument, we can suggest a more specific
error message: "Did you mean to use typeof T?"

adds error message

WIP: Detect error

WIP: progress

updated tests

janky implementation
This suggests 'typeof T' as a potential alternative when we give an error
about using value T as a type.
@amcasey

Copy link
Copy Markdown
Member

Closing in favor of #37359.

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Suggest Did you mean 'typeof Foo' when using value Foo as a type

5 participants