See https://jeengbe.github.io/vality/eslint-plugin-vality for more information.
This page also assumes that you are somewhat familiar with Vality. If not, check that out first.
const Brand = {
logo: v.dict([64, 128, 256], v.url),
};
type Brand = Parse<typeof Brand>;
/* {
logo: {
[x: number]: URL; // Where are the literal values???
};
} */
Can you spot what's wrong with this model? Correct, it's missing the literal types for the keys. This is a common mistake when using Array or Enum Shorts and can be easily fixed by adding as const
. (Find more information on this mistake here).
const Brand = {
logo: v.dict([64, 128, 256] as const, v.url),
};
type Brand = Parse<typeof Brand>;
/* {
logo: {
64: URL;
128: URL;
256: URL;
};
} */
Forgetting this sucks and can quickly become a source of frustration when suddenly types are weird. ESLint to the rescue! It will warn you when you forget to add as const
in places where is may backfire and adds it automatically for you.
Now that you are interested, check out the full documentation for more information.