-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
As operator #3201
As operator #3201
Conversation
@@ -669,6 +670,12 @@ module ts { | |||
right: Expression; | |||
} | |||
|
|||
export interface AsExpression extends Expression { | |||
left: Expression; | |||
asToken: Node; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need this token?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think we need to store the as
token.
Can we have a few tests for templates in var x = `${123 + 456 as number}`;
var y = `leading ${123 + 456 as number}`;
var y = `${123 + 456 as number} trailing`;
declare function tag(...x: any[]): any;
var x = tag `Hello ${123} World` as string;
var y = tag `Hello` as string; |
Should we add formating rule for var x = 42 as string; will become var x = 42 as string; |
Could we add some tests: var a = 20;
var b = a as string;
var as = "hello";
var as1 = as as string; |
I'm just wondering if we can support inferred type assertions and if it is a good idea? Instead of:
We could just use:
And it will infer the type by looking at the last property. |
If by formatting rule, you mean a restriction, yes (good catch!), but not related to the example you just gave (nowhere else in the language do we differentiate whitespace on the same line). Consider the following: class Foo { }
declare function as(...args: any[]);
// Example 1
var x = 10
as `Hello world`
// Example 2
var y = 20
as(Foo); Example 1 is not as much of a problem; you can't use a template string as a type. Example 2 suffers from potentially the same problem as #2995. |
@yuit Ah, by formatting rule, you meant in the LS - still, glad we caught this. |
@DanielRosenwasser yes, I mean from LS side similar to: function foo() {} become function foo() {} Though you example will be a good one to add as well. |
if (token === SyntaxKind.AsKeyword) { | ||
parseTokenNode(); | ||
leftOperand = makeAsExpression(leftOperand, parseType()); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else
on the next line.
Any other feedback? |
@@ -2818,7 +2821,16 @@ module ts { | |||
break; | |||
} | |||
|
|||
leftOperand = makeBinaryExpression(leftOperand, parseTokenNode(), parseBinaryExpressionOrHigher(newPrecedence)); | |||
if (token === SyntaxKind.AsKeyword) { | |||
if (canParseSemicolon()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just add a comment for this case.
// This should be parsed as an initialized variable, followed by a function call to 'as' with the argument 'Bar' | ||
if (canParseSemicolon()) { | ||
break; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else
on the next line
You're currently not contextually typing the left-hand side of the var x = (v => v) as (x: number) => number
var x = (v => v) as (x: number) => string currently typechecks without a problem even though var x = <(x: number) => string>(v => v) gives an error. |
Can we also have the following tests to demonstrate left-associativity of
var x = 10 as number as any as string // should be okay
var y = 10 as string as number; // should error |
Anything else? |
Are we leaving the services layer to a second pass? I can think of at least:
class C<T> {
constructor() {
// C, T, and U should show up below.
let f = <U>(x: any) => x as /**/ type A = any;
namespace n {
type B = any;
// A, B, T, and n should show up below.
let f = <T>(x: any) => x as /**/
|
I am working on a change such that keywords like But It seems like the appropriate thing to do is to treat it as an declaration named You'll have to account for this with some tests like: interface as { } interface
as
{ } interface
as({ }) namespace as {} declare as {} type as = number; |
// as (Bar) | ||
// This should be parsed as an initialized variable, followed | ||
// by a function call to 'as' with the argument 'Bar' | ||
if (canParseSemicolon()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just check scanner.hasPrecedingLineBreak instead? I don't really like canParseSemicolon that much.
@RyanCavanaugh is this ready to go in? |
I'm merging this up with the JSX work, which should have a PR in a day or so |
closing in favor of #3564 |
Implements
as
operator as suggested in #296.