Skip to content
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

Improve error message for using value as type #29778

Closed
evmar opened this issue Feb 6, 2019 · 5 comments · Fixed by #29896
Closed

Improve error message for using value as type #29778

evmar opened this issue Feb 6, 2019 · 5 comments · Fixed by #29896
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@evmar
Copy link
Contributor

evmar commented Feb 6, 2019

TypeScript Version: 3.4.0-dev.20190206

Search Terms: "cannot find name" type error

Code

If you use a type as a value you get a nice error message:

type t = number;
let x = t;
// error TS2693: 't' only refers to a type, but is being used as a value here.

But if you do the same thing in reverse, you get a really confusing one:

let t = 3;
let x: t;
// error TS2304: Cannot find name 't'.

Expected behavior:

Error message should say something more like the type one.

Actual behavior:

Error message suggests there is no 't' in scope at all, which is very confusing especially when you have imported t from some other file and you thought it was a type.

Playground Link:

https://www.typescriptlang.org/play/#src=%0D%0Afunction%20goodError()%20%7B%0D%0A%20%20%20%20type%20t%20%3D%20number%3B%0D%0A%20%20%20%20let%20x%20%3D%20t%3B%20%2F%2F%20good%20error%20message%0D%0A%7D%0D%0A%0D%0Afunction%20badError()%20%7B%0D%0A%20%20%20%20let%20t%20%3D%203%3B%0D%0A%20%20%20%20let%20x%3A%20t%3B%20%2F%2F%20bad%20error%20message%0D%0A%7D

Related Issues:

There are some around this same problem with namespaces I think, like
#26391 or #12075

I think this issue is more broadly complaining about issues in this area:
#27630
But the issue I'm filing here is very specific and concrete, with an easy repro.

This issue discusses some suggested fixes for this issue, but I think first we need to fix the error message.
#28975

@DanielRosenwasser
Copy link
Member

Duplicate of #28975?

@evmar
Copy link
Contributor Author

evmar commented Feb 6, 2019

Hm, that one is really close. But it is saying we should suggest a specific fix, and is bogged down in how it's hard to pick the right fix. Here I'm asking to at least fix the error message (without necessarily suggesting a fix), so #28975 I guess would be follow-on work from that.

Edit: I stuck a link to it in the related list.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Experience Enhancement Noncontroversial enhancements labels Feb 6, 2019
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Feb 6, 2019
@DanielRosenwasser DanielRosenwasser added Help Wanted You can do this Good First Issue Well scoped, documented and has the green light labels Feb 6, 2019
@DanielRosenwasser
Copy link
Member

Seems small enough to avoid the bikeshed 🚲🏠.

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Feb 7, 2019

@DanielRosenwasser Would the message 't' refers to a value, but is being used as a type here. be too similar to the reverse message ? ('t' only refers to a type, but is being used as a value here.)

Also where would be a good place to place the tests for such a feature ?

@DanielRosenwasser
Copy link
Member

Thanks for the PR @dragomirtitian!

@DanielRosenwasser DanielRosenwasser added Fixed A PR has been merged for this issue Domain: Error Messages The issue relates to error messaging labels Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants