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

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 👍 or describe your use case if it’s particularly compelling.

Hope that helps; good luck!

Leave a Comment