Skip to content

As operator - #3201

Closed
Ryan Cavanaugh (RyanCavanaugh) wants to merge 12 commits into
microsoft:masterfrom
RyanCavanaugh:as_operator
Closed

As operator#3201
Ryan Cavanaugh (RyanCavanaugh) wants to merge 12 commits into
microsoft:masterfrom
RyanCavanaugh:as_operator

Conversation

@RyanCavanaugh

Copy link
Copy Markdown
Member

Implements as operator as suggested in #296.

Comment threadsrc/compiler/types.ts Outdated

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member

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.

@DanielRosenwasser

Copy link
Copy Markdown
Member

Can we have a few tests for templates in tests/cases/.../es6/templates/?

varx=`${123+456asnumber}`;vary=`leading ${123+456asnumber}`;vary=`${123+456asnumber} trailing`;
var x = `Hello ${123} World` as string;
var y = `Hello` as string;
var z = 1 + `${1} end of string` as string;
declarefunctiontag(...x: any[]): any;varx=tag`Hello ${123} World`asstring;vary=tag`Hello`asstring;

@yuit

Copy link
Copy Markdown
Contributor

Should we add formating rule for as?
For example:

varx=42asstring;

will become

varx=42asstring;

@yuit

Copy link
Copy Markdown
Contributor

Could we add some tests:

vara=20;varb=aasstring;varas="hello";varas1=asasstring;

@tinganho

Copy link
Copy Markdown
Contributor

I'm just wondering if we can support inferred type assertions and if it is a good idea?

Instead of:

(foo as Foo).(bar as Bar).text

We could just use:

foo.bar.text

And it will infer the type by looking at the last property.

@DanielRosenwasser

Copy link
Copy Markdown
Member

Should we add formating rule for as?

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:

classFoo{}declarefunctionas(...args: any[]);// Example 1varx=10as`Hello world`// Example 2vary=20as(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. as would ordinarily be a function call with a constructor function, but here it is a type assertion on 20 to Foo.

@DanielRosenwasser

Copy link
Copy Markdown
Member

Yui (@yuit) Ah, by formatting rule, you meant in the LS - still, glad we caught this.

@yuit

Copy link
Copy Markdown
Contributor

Daniel Rosenwasser (@DanielRosenwasser) yes, I mean from LS side similar to:

functionfoo(){}

become

functionfoo(){}

Though you example will be a good one to add as well.

Comment threadsrc/compiler/parser.ts Outdated

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.

@RyanCavanaugh

Copy link
Copy Markdown
MemberAuthor

Any other feedback?

Comment threadsrc/compiler/parser.ts Outdated

Copy link
Copy Markdown
Contributor

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.

Comment threadsrc/compiler/parser.ts Outdated

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

@DanielRosenwasser

Copy link
Copy Markdown
Member

You're currently not contextually typing the left-hand side of the as operator. So if you take

varx=(v=>v)as(x: number)=>number

v gets typed as any right now. This means that

varx=(v=>v)as(x: number)=>string

currently typechecks without a problem even though

varx=<(x: number)=>string>(v=>v)

gives an error.

@DanielRosenwasser

Copy link
Copy Markdown
Member

Can we also have the following tests to demonstrate left-associativity of as?

asOperatorAssociativity01.ts

varx=10asnumberasanyasstring// should be okay

asOperatorAssociativity02.ts

vary=10asstringasnumber;// should error

@RyanCavanaugh

Copy link
Copy Markdown
MemberAuthor

Anything else?

@DanielRosenwasser

Copy link
Copy Markdown
Member

Are we leaving the services layer to a second pass? I can think of at least:

  • Formatting tests
  • Does as get occurrence highlighting to point out type assertion locations? (kind of useful; it's the point of static_cast etc. in C++).
  • Completion list tests for arrow functions:
classC<T>{constructor(){// C, T, and U should show up below.letf=<U>(x: any)=>xas/**/
typeA=any;namespacen{typeB=any;// A, B, T, and n should show up below.letf=<T>(x: any)=>xas/**/
  • Completion list tests to ensure that you never have a new identifier location after an as.

@DanielRosenwasser

Copy link
Copy Markdown
Member

I am working on a change such that keywords like interface must be followed by an identifier on the same line to be considered the start of an interface declaration.

But as is only a type assertion if it follows an identifier on the same line. So there's an ambiguity.

It seems like the appropriate thing to do is to treat it as an declaration named as.

You'll have to account for this with some tests like:

interfaceas{}
interfaceas{}
interfaceas({})
namespaceas{}
declareas{}
typeas=number;

Copy link
Copy Markdown
Contributor

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.

@mhegazy

Copy link
Copy Markdown
Contributor

Ryan Cavanaugh (@RyanCavanaugh) is this ready to go in?

@RyanCavanaugh

Copy link
Copy Markdown
MemberAuthor

I'm merging this up with the JSX work, which should have a PR in a day or so

@mhegazy

Copy link
Copy Markdown
Contributor

closing in favor of #3564

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@RyanCavanaugh@DanielRosenwasser@yuit@tinganho@mhegazy@ahejlsberg@CyrusNajmabadi@JsonFreeman@msftclas