question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array.length type guard for array spreading

See original GitHub issue

TypeScript 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:open
  • Created 5 years ago
  • Reactions:16
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
zheeengcommented, Dec 17, 2018

I have the same request:

I want a convenient and easy-to-maintain way to support the future branches-exploding.

function fn0(a: any) { 
    return a
}
function fn1(a: any, b: any) {
    return a
}
function fn2(a: any, b: any, c: any) {
    return a
}
function fn3(a: any, b: any, c: any, d: any) {
    return a
}
function fn4(a: any, b: any, c: any, d: any, e: any) {
    return a
}

function fn(...args: any[]) {
    switch (args.length) {
        case 1:
            return fn0(args[0])
        case 2:
            return fn1(args[0], args[1])
        case 3:
            return fn2(args[0], args[1], args[2])
        case 4:
            return fn3(args[0], args[1], args[2], args[3])
        case 5:
            return fn4(args[0], args[1], args[2], args[3], args[4])
        default:
            throw Error()
    }
}

I expect to refactor fn to the below way:

function fn(...args: any[]) {
    switch (args.length) {
        case 1:
            //Expected 1 arguments, but got 0 or more.
            return fn0(...args)
        case 2:
            return fn1(...args)
        case 3:
            return fn2(...args)
        case 4:
            return fn3(...args)
        case 5:
            return fn4(...args)
        default:
            throw Error()
    }
}
1reaction
weswighamcommented, Dec 6, 2018

More like “we’re waiting for more people to weigh in before we take decisive action in any direction”

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found