Skip to content

Improve error message for using value as type #29778

Closed
@evmar

Description

@evmar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Error MessagesThe issue relates to error messagingExperience EnhancementNoncontroversial enhancementsFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions