GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — As operator by RyanCavanaugh · Pull Request #3201 · microsoft/TypeScript

Conversation

@RyanCavanaugh

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) commented May 18, 2015

Copy link
Copy Markdown
Member

Implements as operator as suggested in #296.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Ryan Cavanaugh (RyanCavanaugh) added 3 commits May 18, 2015 08:12
@RyanCavanaugh
'as' operator
20f5987
@RyanCavanaugh
Emit for 'as' operator
634cc0e
@RyanCavanaugh
Add an error testcase
8f78848
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label May 18, 2015
mhegazy
mhegazy reviewed May 18, 2015
View reviewed changes
Comment thread src/compiler/types.ts Outdated

@mhegazy Mohamed Hegazy (mhegazy) May 18, 2015

Copy link
Copy Markdown
Contributor

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) May 18, 2015

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented May 18, 2015

Copy link
Copy Markdown
Member

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

var x = `${123 + 456 as number}`;
var y = `leading ${123 + 456 as number}`;
var y = `${123 + 456 as number} trailing`;
var x = `Hello ${123} World` as string;
var y = `Hello` as string;
var z = 1 + `${1} end of string` as string;
declare function tag(...x: any[]): any;

var x = tag `Hello ${123} World` as string;
var y = tag `Hello` as string;
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Ryan Cavanaugh (RyanCavanaugh) added 3 commits May 18, 2015 11:04
@RyanCavanaugh
PR feedback
8b03911
@RyanCavanaugh
Add a type alias for As/old-style assert
3aa1caa
@RyanCavanaugh
Correctly deparenthesize 'as' exprs
e1e577d
@yuit

Yui (yuit) commented May 19, 2015

Copy link
Copy Markdown
Contributor

Should we add formating rule for as?
For example:

var x = 42   as string;

will become

var x = 42 as string;
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yuit

Yui (yuit) commented May 19, 2015

Copy link
Copy Markdown
Contributor

Could we add some tests:

var a = 20;
var b = a as string;
var as = "hello";
var as1 = as as string;
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tinganho

Tingan Ho (tinganho) commented May 19, 2015

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented May 19, 2015

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:

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. as would ordinarily be a function call with a constructor function, but here it is a type assertion on 20 to Foo.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented May 19, 2015

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request May 19, 2015
JSX in TypeScript #3203
Closed
@yuit

Yui (yuit) commented May 19, 2015

Copy link
Copy Markdown
Contributor

Daniel Rosenwasser (@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

DanielRosenwasser
DanielRosenwasser reviewed May 20, 2015
View reviewed changes
Comment thread src/compiler/parser.ts Outdated

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) May 20, 2015

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Ryan Cavanaugh (RyanCavanaugh) added 3 commits May 19, 2015 18:04
@RyanCavanaugh
Add formatting for 'as' operator
1e6bd1c
@RyanCavanaugh
Fix ASI for 'as' operator
d5bf689
@RyanCavanaugh
Add test for things named 'as' as suggested
c1e02c4
@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented May 20, 2015

Copy link
Copy Markdown
Member Author

Any other feedback?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

CyrusNajmabadi
CyrusNajmabadi reviewed May 20, 2015
View reviewed changes
Comment thread src/compiler/parser.ts Outdated

@CyrusNajmabadi Cyrus Najmabadi (CyrusNajmabadi) May 20, 2015

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@RyanCavanaugh
CR feedback
d127775
DanielRosenwasser
DanielRosenwasser reviewed May 20, 2015
View reviewed changes
Comment thread src/compiler/parser.ts Outdated

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) May 20, 2015

Copy link
Copy Markdown
Member

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@RyanCavanaugh
Whitespace
9e6e265
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented May 20, 2015

Copy link
Copy Markdown
Member

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

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

v gets typed as any right now. This means that

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented May 20, 2015

Copy link
Copy Markdown
Member

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

asOperatorAssociativity01.ts

var x = 10 as number as any as string // should be okay

asOperatorAssociativity02.ts

var y = 10 as string as number; // should error
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh
Add contextual typing for 'as' and clean up a bunch of missed 'switch…
225e695
…' cases
@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented May 22, 2015

Copy link
Copy Markdown
Member Author

Anything else?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented May 22, 2015

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:
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 /**/ 
  • Completion list tests to ensure that you never have a new identifier location after an as.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request May 27, 2015
Cast to Any #3267
Closed
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jun 4, 2015

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:

interface as { }
interface
as
{ }
interface
as({ })
namespace as {}
declare as {}
type as = number;
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JsonFreeman
JsonFreeman reviewed Jun 6, 2015
View reviewed changes
Comment thread src/compiler/parser.ts

@JsonFreeman Jason Freeman (JsonFreeman) Jun 6, 2015

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhegazy

Mohamed Hegazy (mhegazy) commented Jun 9, 2015

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Jun 10, 2015

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Jun 19, 2015

Copy link
Copy Markdown
Contributor

closing in favor of #3564

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy Mohamed Hegazy (mhegazy) closed this Jun 19, 2015
@danquirk Dan Quirk (danquirk) mentioned this pull request Jun 26, 2015
TypeScript highlighting chokes on some casts highlightjs/highlight.js#878
Closed
@techird techirdliu (techird) mentioned this pull request Nov 4, 2015
[翻译] TypeScript 和 JSX 搞基的事 techird/blog#3
Open
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Feb 17, 2017
as cannot be moved to a new line #14130
Closed
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request May 21, 2018
Fails to compile "x = 2 as number" when "as" starts a new line. #24284
Closed
@microsoft 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.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

9 participants

@RyanCavanaugh @DanielRosenwasser @yuit @tinganho @mhegazy @ahejlsberg @CyrusNajmabadi @JsonFreeman @msftclas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.