Skip to content

Improvement of type checking with array methods "some"/"every" #46894

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
5 tasks done
Eugene-Musika opened this issue Nov 22, 2021 · 4 comments
Closed
5 tasks done

Improvement of type checking with array methods "some"/"every" #46894

Eugene-Musika opened this issue Nov 22, 2021 · 4 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@Eugene-Musika
Copy link

Eugene-Musika commented Nov 22, 2021

Suggestion

🔍 Search Terms

some, every, type check for array values, Array.some(), Array.every()

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Add type guard checking for arrays.

📃 Motivating Example

const funcString = (a: string, b: string) => void;
const funcArray = (a: string[], b: string[]) => void;
const a: string | string[];
const b: string | string[];

if (Array.isArray(a) && Array.isArray(b)) {
	funcArray(a, b); // Works!
}

if ([a, b].every(Array.isArray)) {
	funcArray(a, b); // Error, type "string | string[]" can not be assigned for type "string[]"
}

if (typeof a === 'string' && typeof b === string)) {
	funcString(a, b); // Works!
}

if ([a, b].some(Array.isArray)) {
	return;
}
funcString(a, b); // Error, type "string | string[]" can not be assigned for type "string"
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Nov 22, 2021
@RyanCavanaugh
Copy link
Member

There'd be no way to do this except by hardcoding in detection of this exact pattern which, while cute, seems not prevalent enough in idiomatic JS to warrant the special case.

@Eugene-Musika
Copy link
Author

Eugene-Musika commented Nov 22, 2021

@RyanCavanaugh sorry to hear that... But thanks for response! Perhaps then I should close the issue? Or should it remain open just in case?

@fatcerberus
Copy link

fatcerberus commented Nov 23, 2021

Interestingly, this also doesn't work:

const foo = [a, b] as const;
if (foo.every(Array.isArray)) {
	funcArray(foo[0], foo[1]);  // also error
}

While every does act as a type guard, it narrows it to <TupleType> & readonly any[][] which TS isn't smart enough to figure out means every element is an array. Removing the as const makes the error go away, but then it just "narrows" foo to any[][] which isn't very nice. ☹️

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Too Complex" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

4 participants