Skip to content
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

[Feature Request]Tell the compiler the type #23250

Closed
kgtkr opened this issue Apr 8, 2018 · 4 comments
Closed

[Feature Request]Tell the compiler the type #23250

kgtkr opened this issue Apr 8, 2018 · 4 comments

Comments

@kgtkr
Copy link

kgtkr commented Apr 8, 2018

Background

Currently, it is not possible to tell the compiler the type of the variable.
Although it can be solved by creating new variables, extra overhead occurs.

Proposal

x is Type;

Example

Current

try{
  ...
}catch(ex){
  const e:TypeError=ex;
}

Better

try{
  ...
}catch(e){
  e is TypeError;
}
@billba
Copy link
Member

billba commented Apr 8, 2018

try{
  ...
}catch(e: TypeError){
  ...
}

@kgtkr
Copy link
Author

kgtkr commented Apr 8, 2018

@billba

Catch clause variable cannot have a type annotation.

And, try-catch is an example

@yortus
Copy link
Contributor

yortus commented Apr 9, 2018

@kgtkr for your example it's probably better to use a type guard, since e might be anything:

try {
    // ...
}
catch (e) {
    if (e instanceof TypeError) {
        // e is TypeError here
    }
    else {
        throw e;
    }
}

If you are suggesting a more general proposal, is there something here not already covered by #10421 (type assertion statements at block scope), or just plain type assertions?

@kgtkr
Copy link
Author

kgtkr commented Apr 9, 2018

It was a duplicate of #10421
I did not notice duplication. sorry
Close this issue

@kgtkr kgtkr closed this as completed Apr 9, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants