#How to force given type to be record of value types of previous type values?

1 messages · Page 1 of 1 (latest)

hybrid otter
#

I am trying to force that second given type to have values of type of first generic argument but for some reason I cant make it work

edgy knotBOT
#
interface Foo {
  a: string;
  b: number;
}

interface Bar {
  as: string;
  bb: number;
}

type OnlyMatching<T> = Record<string, T[keyof T]>;

interface Type<A, B extends OnlyMatching<A>> {
  a: A;
  b: B;
}

type Result = Type<Foo, Bar>;
//                      ^^^
// Type 'Bar' does not satisfy the constraint 'OnlyMatching<Foo>'.
//   Index signature for type 'string' is missing in type 'Bar'.
umbral fulcrum
#

Interfaces don't have implicit index signatures, if you change Bar to a type it works.

hybrid otter
#

I see makes sense then I guess