Handle AsExpression in TS when treated as type hint
valorkin opened this issue · 11 comments
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
This is expected because we haven't started implementing TypeScript-specific syntax yet. Happy to get a PR, though. :)
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.
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 :(
@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.
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
@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.
what if can just remove them from AST from now?
so we can use and parse es6/es7 correct syntax?
@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
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)
@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.
@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?