3

Look at this snippet:

type Constrained<T extends { [K in string]: string }> = T;

type A = {
    p: string;
}

interface B {
    p: string;
}

type R1 = Constrained<A>; // Why this is OK...
type R2 = Constrained<B>; // ... and this an Error?

playground

I know the constraint could be expressed differently to avoid the error, for instance,

type Constrained<T extends { [K in keyof T]: string }> = T;

and solutions like this can be found here or here.

The main question is: why the interface fails but the type does not?

My guess is because interfaces are open-ended (they can be extended/merged).

Since the error message is something like

Index signature is missing in type 'B'.

I think this PR is relevant. In that case another question arises:

Is a type declared with type the representation of an object literal type?

Daniel
  • 2,657
  • 1
  • 17
  • 22
  • "object literal type" --- object literal is a way to represent JS object in code, it's not a special type. – zerkms Jul 06 '19 at 09:04

0 Answers0