Skip to content

Conversation

mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Jun 12, 2015

With type predicates in, here is a change to the lib to make it use that in isArray definition.

@vladima
Copy link
Contributor

vladima commented Jun 12, 2015

👍

1 similar comment
@JsonFreeman
Copy link
Contributor

👍

@@ -1150,7 +1150,7 @@ interface ArrayConstructor {
(arrayLength?: number): any[];
<T>(arrayLength: number): T[];
<T>(...items: T[]): T[];
isArray(arg: any): boolean;
isArray(arg: any): arg is Array<any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we allow this to be generic? i.e.

isArray<T>(arg: any): arg is Array<T>

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but we should add a separate overload for it. Otherwise, not passing the type argument gives you Array<{}>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so are you expecting this to work:

var arr: Array<number> | Array<string>;

if (Array.isArray<number>(arr)) {
    arr[0].toFixed(); // but what does that mean at runtime?
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhegazy I think you're right. Specifying the type is claiming to know something that you don't.

mhegazy added a commit that referenced this pull request Jun 13, 2015
Define Array.isArray using a type predicate
@mhegazy mhegazy merged commit 60e855e into master Jun 13, 2015
@mhegazy mhegazy deleted the useTypePredicatesInLib branch June 13, 2015 00:39
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants