Skip to content

Support the new 4.0 definition of isArray - #102413

Merged
Matt Bierner (mjbvz) merged 1 commit into
microsoft:masterfrom
orta:isArray_improvement
Jul 13, 2020
Merged

Support the new 4.0 definition of isArray#102413
Matt Bierner (mjbvz) merged 1 commit into
microsoft:masterfrom
orta:isArray_improvement

Conversation

@orta

Copy link
Copy Markdown
Contributor

This PR relates to microsoft/TypeScript#39258

Effectively, it brings your isArray type check inline with the version in this PR ^

Example:

// Original version, uses any exportfunctionisArrayOld(array: any): array is any[]{returnArray.isArray(array);}// New, funky-looking, but retains readonly safelyfunctionisArray<T>(array: T|{}): array is Textendsreadonlyany[] ? (unknownextendsT ? never : readonlyany[]) : any[]{returnArray.isArray(array);}constmutableArray: string[]=[]constimmutableArray: readonlystring[]=[]if(isArray(mutableArray)){mutableArray.push("")}// Fails correctlyif(isArray(immutableArray)){immutableArray.push("")// would error here}/// Previous behaviorif(isArrayOld(mutableArray)){mutableArray.push("")}if(isArrayOld(immutableArray)){immutableArray.push("")// No error on pushing to an readonly array}

Playground Link

@mjbvz
Matt Bierner (mjbvz) merged commit d47ddb6 into microsoft:masterJul 13, 2020
@mjbvz

Copy link
Copy Markdown
Contributor

Thanks!

@mjbvzMatt Bierner (mjbvz) added this to the July 2020 milestone Jul 13, 2020
Charles Gagnon (Charles-Gagnon) pushed a commit to Charles-Gagnon/vscode that referenced this pull request Jul 14, 2020
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 27, 2020
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.

2 participants

@orta@mjbvz