Typescript: Why type alias satisfies a constraint but same interface doesn't?

后端 未结 1 1314
广开言路
广开言路 2020-12-19 07:17

I ran into this weird case. I declare a conditional type. For the same extends constraint, a type alias satisfies it, while a structurally identical interface d

相关标签:
1条回答
  • 2020-12-19 07:50

    You've run into a known issue, microsoft/TypeScript#15300 whereby implicit index signatures are inferred for type aliases but not for interfaces. This is one of those few places where type aliases and interfaces differ in type analysis. According to @RyanCavanaugh (Development lead for the TypeScript team at Microsoft), this is by design:

    Just to fill people in, this behavior is currently by design. Because interfaces can be augmented by additional declarations but type aliases can't, it's "safer" (heavy quotes on that one) to infer an implicit index signature for type aliases than for interfaces. But we'll consider doing it for interfaces as well if that seems to make sense. [emphasis added]

    Okay, it's currently by design but the GitHub issue's status (as of 2019-04-23) is "suggestion" and "in discussion". So if you want to see this changed, you might want to go there and give the issue a

    0 讨论(0)
提交回复
热议问题