Consider the TypeScript code below:
type example = 'BOOLEAN' | 'MULITSELECT' | '';
interface IObjectExample {
a: string,
readonly b: example
}
const handleObj = (obj: IObjectExample) :void => {
console.log('have obj', obj);
}
const actualExample = {
a: 'hello',
b: 'BOOLEAN'
};
handleObj(actualExample); // error pops up here
An error is being shown as:
Argument of type '{ a: string; b: string; }' is not assignable to parameter
of type 'IObjectExample'. Types of property 'b' are incompatible.
Type 'string' is not assignable to type 'example'.
Refer to playground.
This could be due to the 'widened literal type of the expression' for 'BOOLEAN'
. Is this caused by not using let
, but having a readonly
property?
The main question is how to ensure a specific string literal as a property value, perhaps using an Enum
?