Microsoft/TypeScript

Conditional type prevents assignability

Open

#48,033 创建于 2022年2月25日

在 GitHub 查看
 (11 评论) (2 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: Conditional TypesHelp Wanted

仓库指标

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

描述

Bug Report

🔎 Search Terms

Index signature assignability, generic index signatures, conditional types

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types

⏯ Playground Link

Playground link with relevant code

💻 Code

// Use of the conditional type `string extends T` prevents assignability.
// However, the conditional type `T extends string` is accepted.

declare class Foo<T>  {
    bar: string extends T ? number : boolean;
}

declare let indexable: Foo<{[key: string]: boolean}>;
declare let namedProp: Foo<{baz: boolean}>;

indexable = namedProp;

🙁 Actual behavior

When a conditional type is present in Foo, I sometimes cannot assign Foo<{a: any}> to Foo<[x: string]: any>, depending on the condition.

🙂 Expected behavior

Regardless of the conditional type, I should be able to assign Foo with named properties to Foo with an index signature.

贡献者指南