Skip to content

Better error message for "Cannot find name" when looking for a type #7900

Closed

Description

@a8775

TypeScript Version:
1.8.7

Code

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "declaration": true
    },
    "files": [
        "main.ts",
        "mytypes.ts"
    ]
}

main.ts

import {MyClassAlias} from './mytypes';

let a: MyClassAlias = new MyClassAlias();

mytypes.ts

export class MyClass {
    public a: number;
}

export type MyClassAlias = MyClass;

Expected behavior:
Object created using alias type.

Actual behavior:
Compilation error:

main.ts(3,27): error TS2304: Cannot find name 'MyClassAlias'.

in vscode (0.10.11) syntax is marked as error as well:

snip_20160406234306

but IntelliSense seems to work:

snip_20160406234539

Activity

  1. jwbay commented on Apr 6, 2016

    @jwbay
    Contributor

    a type alias is a compile-time construct. there's no javascript emitted for it, so you can't new it up.

  2. RyanCavanaugh commented on Apr 6, 2016

    @RyanCavanaugh
    Member

    This is working as intended (as noted, type is only a type; use a const + type pair if you want both definitions), but we should give a better error message when this happens like we do in other cases where symbol resolution fails in one namespace but would succeed in another.

  3. added this to the milestone on Apr 6, 2016
  4. changed the title [-]Cannot create object using alias[/-] [+]Better error message for "Cannot find name" when looking for a type[/+] on Apr 7, 2016
  5. hleimig commented on Jul 23, 2016

    @hleimig

    What do you guys think of

     Cannot instantiate type 'MyClassAlias'. Operator new cannot be used with alias types.
    
  6. RyanCavanaugh commented on Jul 25, 2016

    @RyanCavanaugh
    Member

    This error message should be general to all cases where you refer to an identifier in a value position which only resolves to something in the type namespace. In other words:

    interface JustAType { }
    var x = JustAType; // Error: __(fill in the blank)__

    I was thinking something like

    Cannot find name 'JustAType'. A type exists with this name, but no value.

  7. modified the milestones: TypeScript 2.1, on Sep 13, 2016
  8. mhegazy commented on Sep 13, 2016

    @mhegazy
    Contributor

    thanks jwbay!

  9. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issueFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions