TypeScript Version: 3.9
Search Terms:
Mapped types
Infer specific value of mapped type
Code
//? This type should be a map between keys of state (S) and keys of model (M)
//! A single mapping (key-value pair) should be possible only if
//! the state property (S[P]) and the model property (M[specific keyof M]) are of the same type
type Wiring<S extends object, M extends object> = {
[P in keyof S]?: S[P] extends M[keyof M] ? keyof M : never;
};
// Concrete example of state
type State = {
dogNose: string;
};
// Concrete example of model
type Model = {
earColor: string;
hairColor: string;
nose: string;
};
// Concrete example of wiring
const wiring: Wiring<State, Model> = { dogNose: "nose" }
Expected behavior:
What I would need is something along the lines of:
type Wiring<S extends object, M extends object> = {
[P in keyof S]?: M[K in keyof M] extends S[P] ? K : never;
};
Basically, I would need to extract the keys K of M that extend the type of S[P] (note the M[K in keyof M]).
Is this possible?
TypeScript Version: 3.9
Search Terms:
Mapped types
Infer specific value of mapped type
Code
Expected behavior:
What I would need is something along the lines of:
Basically, I would need to extract the keys K of M that extend the type of S[P] (note the M[K in keyof M]).
Is this possible?