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?
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 interface
s 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?