Array.length type guard for array spreading
See original GitHub issueTypeScript Version: 3.3.0-dev.20181204
Search Terms: array length type guard spread
Code
const timeString = '09:00';
const date = new Date();
const timeParts = timeString.split(':').map((part) => parseInt(part, 10));
if (timeParts.length > 0) {
date.setHours(...timeParts);
}
Expected behavior: No errors
Actual behavior: TS2556: Expected 1-4 arguments, but got 0 or more.
Playground Link: https://www.typescriptlang.org/play/#src=const timeString %3D ‘09%3A00’%3B const date %3D new Date()%3B const timeParts %3D timeString.split(‘%3A’).map((part) %3D> parseInt(part%2C 10))%3B%0D%0Aif%20(timeParts.length%20%3E%200)%20%7B%0D%0A%20%20%20%20date.setHours(…timeParts)%3B%0D%0A%7D%0D%0A
Related Issues: None
Issue Analytics
- State:
- Created 5 years ago
- Reactions:16
- Comments:10 (6 by maintainers)
Top Results From Across the Web
typescript - Array type guard trick - Stack Overflow
I have 2 questions about that: Does it exist a better way to guard an array type ? Can I link func to...
Read more >Tuple For Type and Length Arrays - Learn TypeScript
Tuple For Type and Length Arrays. In this lesson, we will discuss tuples and how TypeScript differentiates them from arrays. We'll cover the...
Read more >TSConfig Option: downlevelIteration - TypeScript
Example: Effects on Array Spreads. This is an array spread: js. // Make a new array who elements are 1 followed by the...
Read more >Uses of Class java.lang.invoke.MethodHandle (Java SE 18 & JDK 18)
arrayType, int arrayLength). Makes an array-spreading method handle, which accepts an array argument at a given position and spreads its elements as ...
Read more >Inferring types in a conditional type - Learn TypeScript
Consider the simple example below which gives the array element type if the type is an array: Copy. type ArrayElementType<T> = T extends...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have the same request:
I want a convenient and easy-to-maintain way to support the future branches-exploding.
I expect to refactor
fn
to the below way:More like “we’re waiting for more people to weigh in before we take decisive action in any direction”