I have following type:
type Example = {
key1: number,
key2: string
}
and I need to create type based on Example
type to be one of key: value
pair. Of course I'm looking for generic solution. Here is what this type should return:
type Example2 = { ... };
const a: Example2 = { key3: 'a' } // incorrect
const b: Example2 = { key1: 'a' } // incorrect
const c: Example2 = { key2: 1 } // incorrect
const d: Example2 = { key1: 1 } // correct
const e: Example2 = { key2: 'a' } // correct
const f: Example2 = { key1: 1, key2: 'a' } // incorrect
I was trying using this:
type GetOne<T> = { [P in keyof T]: T[P] };
type Example2 = GetOne<Example>;
but it returns all the properties and example with const f
not working as expected.