When working with Typescript, I encountered an issue with assigning a value to the variable op
as a string. The error message "Type 'string' is not assignable to type 'D'" appears. How can I check the type and correctly assign a value?
export const t = <IsMulti extends boolean = false>(): void => {
const value = 'test';
type S = string;
type D = IsMulti extends true ? S[] : S;
const op: D = value;
console.log(op);
};
I attempted to resolve this by adding a parameter of the same type IsMulti
and implemented a conditional check based on it.
export const t = <IsMulti extends boolean = false>(isMulti: IsMulti): void => {
const value = 'test';
type S = string;
type D = IsMulti extends true ? S[] : S;
if (!isMulti) {
const op: D = value;
console.log(op);
}
};
However, the error persisted even after making these adjustments. https://i.sstatic.net/0kBxI.png