1

Let's have the following code:

type FooType = { foo: string }

function fooType(a: FooType & Partial<Record<string, string>>) {

}

function barType(a: FooType) {
    fooType(a)
}

interface FooInterface { foo: string }

function fooInterface(a: FooInterface & Partial<Record<string, string>>) {

}

function barInterface(a: FooInterface) {
    fooInterface(a) // <-- ERROR here ts2345
    // Argument of type 'FooInterface' is not assignable to parameter of type 'FooInterface & Partial<Record<string, string>>'.
    // Type 'FooInterface' is not assignable to type 'Partial<Record<string, string>>'.
    // Index signature for type 'string' is missing in type 'FooInterface'.
}

Link to playgroud

Why FooInterface produces TS2345 error, but FooType not?

TN.
  • 18,874
  • 30
  • 99
  • 157
  • 1
    Does this answer your question? [Typescript: Index signature is missing in type](https://stackoverflow.com/questions/37006008/typescript-index-signature-is-missing-in-type) – Matthieu Riegler Jul 16 '22 at 10:12
  • @MatthieuRiegler No, this is a different situation. – TN. Jul 16 '22 at 13:42

1 Answers1

0

The issue seems to be tracked by https://github.com/microsoft/TypeScript/issues/15300.

Clarification by https://github.com/RyanCavanaugh:

... 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

There are some other differences between type aliases and interface that might affect your choice - namely you can extend an interface but not a type alias. So it's not quite that simple, but as rules of thumb go it's not a bad one.

TN.
  • 18,874
  • 30
  • 99
  • 157