To the best of my understanding TypeScript views a const string
variable as an immutable typed variable with only that value and no other possible value. I always thought that adding as const
to that was redundant.
Why am I getting the following in the 2nd part of the example?
Argument of type 'string' is not assignable to parameter of type...
Example:
declare function each<T extends [any] | any[]>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T) => any, timeout?: number) => void;
const foo1 = 'FOO' as const;
const bar1 = 'BAR' as const;
declare function action1(value: typeof foo1 | typeof bar1): void;
each([
[foo1],
])('test name', (value) => {
// okay
action1(value);
});
const foo2 = 'FOO';
const bar2 = 'BAR';
declare function action2(value: typeof foo2 | typeof bar2): void;
each([
[foo2],
])('test name', (value) => {
// Argument of type 'string' is not assignable to parameter of type '"FOO" | "BAR"'.(2345)
action2(value);
});