dsherret / dsherret/ts-morph

Variadic tuples are considered non-variadic

Open
#1,314 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 15.1.0

When an array contains rest syntax `ts-morph` considers the element to be a tuple. This _might_ be correct, as TypeScript would call it a variadic tuple. However, there seems to be no way to determine that the type is variadic.

**To Reproduce**

```ts
// A.ts
type A = [unknown, ...unknown[]]
```

```ts
import { Project } from 'ts-morph'

const project = new Project()
const sourceFile = project.addSourceFileAtPath(`${__dirname}/A.ts`)
const type = sourceFile.getTypeAlias('A')?.getType()

// reproduce the problem here
console.info(type?.isTuple()) // true
console.info(type?.getTupleElements().length) // 2
console.info(type?.getTupleElements()[1].isArray()) // false
console.info(type?.getTupleElements().map((x) => x.getText())) // ['unknown', 'unknown']
```

**Expected behavior**

As the type suggests that it can take any number of items, it should not be considered a straight forward tuple, but either an array or a variadic tuple.

Contributor guide

Open the contributing guide

Research direction

Start with the reproduced type alias in A.ts and trace the getTypeAlias('A')?.getType() result through isTuple(), getTupleElements(), and the element isArray() checks. Compare the behavior for [unknown, ...unknown[]] with the expected variadic-tuple behavior, then verify that the type exposes a reliable distinction and that the reported reproduction produces the intended result.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.