Having trouble with this code snippet:
type FormatTypes = {
text: string,
binary: Array<number>
};
type Format = keyof FormatTypes;
type FormatType<F extends Format> = FormatTypes[F];
type Formatter = {
format<F extends Format>(param: string, format: F): FormatType<F>
}
const blah: Formatter = {
format(param, format) {
return format === 'text' ? param : [1, 0, 0, 1];
}
};
Why am I getting this error message?
Type '<F extends "text" | "binary">(param: string, format: F) => string | number[]' is not assignable to type '<F extends "text" | "binary">(param: string, format: F) => FormatType<F>'.
Type 'string | number[]' is not assignable to type 'FormatType<F>'.
Type 'string' is not assignable to type 'FormatType<F>'.
Type 'string' is not assignable to type 'string & number[]'.
Type 'string' is not assignable to type 'number[]'.(2322)
input.tsx(10, 5): The expected type comes from property 'format' which is declared here on type 'Formatter'
Any ideas why the conditional statement is not properly refining the type of the format
parameter for the return value? Any suggestions on how to achieve the desired outcome?