Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Handle AsExpression in TS when treated as type hint #25

Closed
valorkin opened this issue Mar 11, 2016 · 11 comments
Closed

Handle AsExpression in TS when treated as type hint #25

valorkin opened this issue Mar 11, 2016 · 11 comments

Comments

@valorkin
Copy link

In typescript you can give a hint to tsc about variable type
using keyword as

(node as ts.BinaryExpression).operatorToken;

currently it throws an exception

@nzakas
Copy link
Member

nzakas commented Mar 11, 2016

This is expected because we haven't started implementing TypeScript-specific syntax yet. Happy to get a PR, though. :)

@nzakas
Copy link
Member

nzakas commented Mar 11, 2016

In order to address this, we need to determine what type of AST node should represent this construct.

If I understand this correctly, the as operator is a casting operation, so I think we should target something that is similar to what Flow does. Here's the definition for Flow type cast node:

interface TypeCastExpression extends Expression {
    expression: Expression;
    typeAnnotation: TypeAnnotation;
  }

The syntax for Flow type casts looks more like foo:bar, so it has a true type annotation whereas TypeScript uses as, which isn't so much a type annotation as it is a type reference. So maybe we should look at doing something like this:

interface TSTypeCastExpression extends Expression {
    expression: Expression;
    typeCast: TypeAnnotation
  }

Unfortunately, we haven't yet defined what TypeAnnotation should be represented by in this project, so I think we'll have to hold off on implementing this until we have that discussion.

See #13 for the current status of the development plan.

@valorkin
Copy link
Author

If I understand this correctly

Typescript has several ways of type hinting

so I think we'll have to hold off on implementing this until we have that discussion

thing is I want get it working fast at least for what is used in angular 2

so must probably I will have to use mine repo :(

@nzakas
Copy link
Member

nzakas commented Mar 11, 2016

@valorkin sorry, we just can't skip ahead to do this. What you have will make the parser continue without error, but you're producing a correct AST, so it will likely cause unexpected errors when used with ESLint.

@nzakas nzakas added enhancement and removed bug labels Mar 11, 2016
@valorkin
Copy link
Author

I am not yet AST maniac, I see some wrong tokens
but I am definitely will need some finger pointing and help
with this to get things done correctly

so If you can say precise what is wrong with it,
it would be good

actually AST for a.b should be equal to (a as node).b
@nzakas correct?
so I will try to achieve it

@nzakas
Copy link
Member

nzakas commented Mar 11, 2016

@valorkin please see my previous comment. Even we create a node type for casting, we still need to create the node types for all types and be able to parse all types. We just aren't there yet, so I'm afraid you'll need to wait until we get to phase 4 of the development plan.

@valorkin
Copy link
Author

what if can just remove them from AST from now?
so we can use and parse es6/es7 correct syntax?

@valorkin
Copy link
Author

@nzakas It seems that I moved you to some misconception
as here is not a type annotation, it is a type hint
If you will have time check this (quick look on code samples)
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#1.2
chapters 1.2 1.3 1.4

this is how type annotations looks in TS

interface Point {  
    x: number;  
    y: number;  
}

function getX(p: Point) {  
    return p.x;  
}

class CPoint {  
    x: number;  
    y: number;  
    constructor(x: number,  y: number) {  
        this.x = x;  
        this.y = y;  
    }  
}

as operator used in 2 situations import/export, type hints
I did some in deep look on how TS works with this
so for (a as b).text where b can be class or interface with property\getter\method\etc...
a as b will be parsed by typescript to
ExpressionStatement:198 -> AsExpression:192 -> Identifier:69 (text: 'a')
only mention of b is in type property of AsExpression
AsExpression.type -> TypeReference:152 .typeName -> Identifier:69 (text: 'b')

something like this

@valorkin
Copy link
Author

class Test {}
interface Point {
    y: Test;
}

part for y: Test will be converted to PropertySignature with name property( eq Identifier) and type property (eq TypeReference)

@nzakas
Copy link
Member

nzakas commented Mar 12, 2016

@valorkin this project is not ready to be used (the readme says it's experimental), so I'm not willing to begin making changes like this yet. You can feel free to make whatever changes you want in your own fork, we just can't start forcing syntax in at this point. Creating the correct AST is important for ESLint to work correctly, which is why we have to take the time to do it right.

@valorkin
Copy link
Author

@nzakas roger that
than I will make my fork just good enough to work with typescript
here is ast explorer to play with it, so it will be easier to reason about
http://valorkin.github.io/astexplorer/#/gvbyXRep3E
most probably when I am done I will be ready to propose AST structure to handle
TypeScript specific things, do you have preferable format for it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants