microsoft / microsoft/TypeScript

Property '[Symbol.iterator]' is missing in type 'PartialTuple<ConstructorParams<T>>

Open
#36,012 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.1 -> 3.7

Search Terms:
tuple, mapped tuple

Code

type Constructor = new (...args: any[]) => any;
type ConstructorParams<T extends Constructor> = T extends new (...args: infer P) => any ? P : never;
type PartialTuple<T extends any[]> = {[I in keyof T]: T[I] | undefined}
type ConstructorReturnType<T extends Constructor> =
    T extends new (...args: any[]) => infer R ? R : never;

function getClassFactory<T extends Constructor>(type: T) {
    type OptionalParams = PartialTuple<ConstructorParams<T>>;
    return getFunctionWithOptionalTypes<OptionalParams, ConstructorReturnType<T>>();
}

Expected behavior:
should compile correctly

Actual behavior:
shows error:

Type 'PartialTuple<ConstructorParams>' does not satisfy the constraint 'any[]'.
Property '[Symbol.iterator]' is missing in type 'PartialTuple<ConstructorParams>'.

Playground Link:
playground

I have to use 2 functions as I get A rest parameter must be of an array type. if I try to do it one function:

function getClassFactory<T extends Constructor>(type: T): new (...args: PartialTuple<ConstructorParams<T>>) => ConstructorReturnType<T> {
    return {} as any;
}

playground

I am trying to create a type that returns a function with parameters equal to that of a class constructor but where each parameter is T[P] | undefined.
I've done similar to this before

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the error in the two linked TypeScript Playgrounds across the stated TypeScript versions, focusing on the mapped tuple and constructor-parameter examples. Trace the compiler's handling of the PartialTuple constraint and rest-parameter validation. Done means both examples compile without the reported '[Symbol.iterator]' or array-type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.