If I create this constant in TypeScript:
const PARAMS = {
green: {color: 0x007700},
black: {color: 0x000000},
white: {color: 0xffffff},
};
it deduces the type as { green: {color: number}; black: { color: number }; white: { color: number }; }
.
But I know from context that the type of the values should be THREE.MeshBasicMaterialParameters
. I can declare this using an indexed type:
const PARAMS: {[name: string]: THREE.MeshBasicMaterialParameters} = {
green: {color: 0x007700},
black: {color: 0x000000},
white: {color: 0xffffff},
};
but then I lose the specific possibilities for keys ("green", "black" or "white"). I can model it more precisely by writing out the full type that I want:
const PARAMS: {
green: THREE.MeshBasicMaterialParameters,
black: THREE.MeshBasicMaterialParameters,
white: THREE.MeshBasicMaterialParameters
} = {
green: {color: 0x007700},
black: {color: 0x000000},
white: {color: 0xffffff},
};
but this is quite verbose and repetitive.
Is there a way to get the the type I want in a more concise way?