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

Compiler API getTypeAtLocation(node) doesn't return Type Alias #6297

Closed
drudru opened this issue Dec 30, 2015 · 7 comments
Closed

Compiler API getTypeAtLocation(node) doesn't return Type Alias #6297

drudru opened this issue Dec 30, 2015 · 7 comments
Labels
API Relates to the public API for TypeScript By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Suggestion An idea for TypeScript

Comments

@drudru
Copy link

drudru commented Dec 30, 2015

First, thanks for making TypeScript. I'm really enjoying this language.

I opened this issue because I'm experiencing an inconsistency or bug in the compiler API surrounding Type Aliases. I like Type Aliases since they are the only way to allow me to differentiate built-in types like number, string, etc. Let me illustrate the issue below.

If I have a class like:

type MyString = string;

class Test
{
    prop1:MyString;

    method() {
        let w = this.prop1;
        this.prop1 = "foo";
    }
}

and I compile it, I will get an 'AST'. Here is a portion of the AST.

      Block
        VariableStatement
          VariableDeclarationList
            VariableDeclaration
              Identifier
              PropertyAccessExpression
                ThisKeyword
                DotToken
                Identifier
        ExpressionStatement
          BinaryExpression
            PropertyAccessExpression
              ThisKeyword
              DotToken
              Identifier
            FirstAssignment
            StringLiteral

When I query the VariableStatement or the ExpressionStatement nodes for type via the compiler API using:

tc.getTypeAtLocation(node)

I don't get 'MyString'. I get 'string'.

However, the PropertyDeclaration node has a Type node that properly returns 'MyString'.
If I hover over the statements in Visual Studio Code, I also only see the type 'string'.

To me, this appears like either a bug or a mistake in my attempts at trying to get the information.

I can provide a sample project if needed too.

@DanielRosenwasser
Copy link
Member

This seems partially related to #6070.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript API Relates to the public API for TypeScript labels Dec 30, 2015
@DanielRosenwasser
Copy link
Member

A reference to a type aliases is meant to eagerly resolve to its aliased type, so we'd have to change the way type aliases themselves are understood within the compiler. I think that's more a technical problem than a philosophical one.

@drudru
Copy link
Author

drudru commented Dec 30, 2015

@DanielRosenwasser - thanks for your response. I think I'll try a different tact in the meantime. So you can close this one if you need to.

Is there a good way to subclass the built-in number and string, yet allow those instances to be used in binary expressions with '+-*/' operators? I just looked at #2031 and #2365.

@DanielRosenwasser
Copy link
Member

I think you want to look at #4372.

@drudru
Copy link
Author

drudru commented Jan 1, 2016

@DanielRosenwasser - that is very helpful - thanks again and Happy New Year

@DanielRosenwasser
Copy link
Member

You're very welcome, have a happy new year!

@mhegazy
Copy link
Contributor

mhegazy commented Jan 8, 2016

This is actually by design. the alias is not a separate type. There is only one type, string in this case, MyString is just a pointer to it, and is not propagated.

@mhegazy mhegazy closed this as completed Jan 8, 2016
@mhegazy mhegazy added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jan 8, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Relates to the public API for TypeScript By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants