Skip to content
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

Union argument type with and spread operator #58513

Open
mageekguy opened this issue May 13, 2024 · 0 comments
Open

Union argument type with and spread operator #58513

mageekguy opened this issue May 13, 2024 · 0 comments

Comments

@mageekguy
Copy link

πŸ”Ž Search Terms

spread, argument, union, type.

πŸ•— Version & Regression Information

It is a "strange" and maybe abnormal behavior of type checking algorithm when union is used on function/method argument with spread operator.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDjAVzABtgAeAZwQC9gA+OAbwCg425tiFgUAxaAHIEAtgCM0FAJIUAFADoFcJCPFQKALiUq0AbQC6cAGRM4pJAHMYAC01VacAL4BKTQDcICACYBuZg+bMnJgUFHCYSACeACpEpJQ09IjCJMDCPDChhCnxtAws7HBgUAiumKhaYhKaypVQ+kYmZpY2cHZ4Dr6s7NgQSBQwUATYMNDyijWqGhWq9caMpjzNtglwTkxdBWzWCBRyExJwALzTEr4F-hscXOn8UEK1UrKc3CiaY3InatXadQZzCxZrMt7M4jgx3F4XHAIZ51ptutcUO98FYdnsfhQnGd2P4LkEQvhiil4KBUEhPKFwtFYuQAMx5S49PoDIYjKAyJAARm+tSOcAADAAaJQAJh5qj5QqUtPFaEla3y8NaRDQHM5wqQIo1tKxl1xASZ-TgnmAWAIxBgUSJpHgxyQwAA7oSEMSZLrmCazRarS6bZc5M8boIMdIZJcCmqNVrpWtDnkOL0KBBSHJiBBzJHRdqsY5Lk5mN4gA

πŸ’» Code

interface tuple<size> {
    clientForNumbersIs(... numbers: number[] & { length: size }): void;
}

class anyTuple<size> implements tuple<size> {
    private numbers: number[] & { length: size };

    constructor(... numbers: number[] & { length: size ) {
        this.numbers = numbers;
    }

    clientForNumbersIs(client: (... numbers: number[] & { length: size }) => void): void {
        client(... this.numbers);
    }
}

class triplet extends anyTuple<3> {
    constructor(n1: number = 0, n2: number = 0, n3: number = 0) {
        super(n1, n2, n3);
    }
}

const defaultTriplet = new triplet();

defaultTriplet
    .clientForNumbersIs(
        (n1, n2, n3) => { console.log(n1, n2, n3); }
    )
;

πŸ™ Actual behavior

The code generate these following errors:

Property 'clientForNumbersIs' in type 'anyTuple<size>' is not assignable to the same property in base type 'tuple<size>'.
  Type '(client: (...numbers: number[] & { length: size; }) => void) => void' is not assignable to type '(...numbers: number[] & { length: size; }) => void'.
    Types of parameters 'client' and 'numbers' are incompatible.
      Property '0' is missing in type 'number[] & { length: size; }' but required in type '[client: (...numbers: number[] & { length: size; }) => void]'.
Argument of type 'number[]' is not assignable to parameter of type 'number[] & { length: size; }'.
  Type 'number[]' is not assignable to type '{ length: size; }'.
    Types of property 'length' are incompatible.
      Type 'number' is not assignable to type 'size'.
        'size' could be instantiated with an arbitrary type which could be unrelated to 'number'.
Argument of type '(n1: number, n2: number, n3: number) => void' is not assignable to parameter of type '(...numbers: number[] & { length: 3; }) => void'.
  Types of parameters 'n1' and 'numbers' are incompatible.
    Type 'number[] & { length: 3; }' is not assignable to type '[n1: number, n2: number, n3: number]'.

However, from my point of view, there is no problem with type because this.numbers in anyTuple match requirement of interface tuple, and it is the same for this.numbers from triplet instance.
So, i see two options:

  • I haven't understood in the right way the type checking algorithm or its philosophy;
  • There is a problem in the type checking algorithm in this context.

So, is there someone here to show me the light ;), or confirm the bogus behavior?

πŸ™‚ Expected behavior

No error.

Additional information about the issue

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant