Skip to content

Missing type guard with Arrays #9469

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

Closed
HolgerJeromin opened this issue Jul 1, 2016 · 5 comments
Closed

Missing type guard with Arrays #9469

HolgerJeromin opened this issue Jul 1, 2016 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@HolgerJeromin
Copy link
Contributor

TypeScript Version: 1.8.0

Code

var myAny: any = 'anythinghere';

if (Array.isArray(myAny)) {
    var newarray: string = myAny;
}

Expected behavior:
myAny should be of type any[] and not any after the check.
Tested with Visual Studio 2015 Update 2 and TypeScript Playground mouse hover.
And compile should break.

@kitsonk
Copy link
Contributor

kitsonk commented Jul 1, 2016

any is never narrowed... expected TypeScript behaviour

If the type was any[] | string then it could be narrowed.

@HolgerJeromin
Copy link
Contributor Author

OK, thanks for the explanation.
Can we change that? :-)

@kitsonk
Copy link
Contributor

kitsonk commented Jul 2, 2016

Changing any to be narrowed would be a gigantic breaking change. In order to have TypeScript be compatible with JavaScript, it needs a universal top type like any. If you want to keep it from not accepting any value, don't assign that as the variable type.

Why is assigning a type other than any a challenge/issue?

@DanielRosenwasser DanielRosenwasser added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 2, 2016
@DanielRosenwasser
Copy link
Member

Can we change that? :-)

Unfortunately, as @kitsonk said, it broke a lot for when people do something like myAny instanceof Object, because you basically can't get any distinct information off of an Object.

See #5011 for when I tried to address this.

@HolgerJeromin
Copy link
Contributor Author

Thanks everyone!

@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.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants