If I have an interface like this:
interface OptionsType {
name: string;
}
Then when I try to use that interface with a property that is not defined, I get Type '{ value: string; disabled: boolean; }' is not assignable to type 'OptionsType'. Object literal may only specify known properties, and 'disabled' does not exist in type 'OptionsType'.
This is good, I want this error to occur when I make this mistake. My problem comes from when I use this in Array.map, it does not give me the error. How can I make it so that my usage of Array.map is properly typed?
Errors properly:
const optionsArr: OptionsType[] = [
{
value: 'hello',
disabled: false, // error
},
];
const options: OptionsType = {
value: 'thing',
disabled: true, // error
};
Does not error:
const optionsArrMapped: OptionsType[] = [1, 2].map(num => ({
value: 'idk',
disabled: true,
}));
// As an example, using `as` prevents the error from happening. I'm not less concerned about this case.
const optionsAs: OptionsType = {
value: 'thing',
disabled: true,
} as OptionsType;