0

How can I define a type for an object where the keys will come from list that I know, in this case field_1 and field_2

type FooKey = 'field_1' | 'field_2';
interface FooValue {
  src: string;
  id: number;
}
interface Foo {
  [FooKey]: FooValue;
}

const thing: Foo = {
  field_1: {
    src: 'src 1',
    id: 1,
  },
  field_2: {
    src: 'src 2',
    id: 2,
  },
};
1
  • Add every field to the type and make them optional. Commented Nov 29, 2021 at 14:35

1 Answer 1

1

Simply use Record

const d: Record<FooKey, FooValue > = {...}
Sign up to request clarification or add additional context in comments.

1 Comment

@Evanss did this answer your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.