Microsoft/TypeScript

Indexing tuple intersection type beyond length produces unexpected type

Open

#42,557 创建于 2021年1月30日

在 GitHub 查看
 (1 评论) (3 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: IntersectionHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Bug Report

🔎 Search Terms

tuple intersection index type

⏯ Playground Link

Playground Link

💻 Code

type T = [number, boolean] & { x: string };
declare const t: T;
const a = t[0]; // number
const b = t[1]; // boolean
const c = t[2]; // number | boolean !?

🙁 Actual behavior

c is typed as number | boolean

🙂 Expected behavior

c should be typed as undefined

I'm not sure if this is a bug, or intended behavior. But it was surprising. If the type is just a tuple, indexing beyond its length produced undefined. But if it is part of an intersection, the element type is changed to number | boolean, as if it is no longer a tuple, but an array. However, indexing 0 or 1 still produces the correct type for those element positions.

贡献者指南