const createSuffixName = <N extends string>(name: N) => {
return <S = undefined>(suffix?: S): S extends string ? `${N}/${S}` : N => {
return suffix ? `${name}/${suffix}` : name;
};
};
const test = createSuffixName<'test'>('test');
const testName1 = test();
// testName1 type = "test"
const testName2 = test<'suffix'>('suffix');
// testName2 type = "test/suffix"
there is an error:
return suffix ? `${name}/${suffix}` : name;
--
TS2322: Type 'N | `${N}/${string}`' is not assignable to type 'S extends string ? `${N}/${S}` : N'.
Type 'N' is not assignable to type 'S extends string ? `${N}/${S}` : N'.
Type 'string' is not assignable to type 'S extends string ? `${N}/${S}` : N'.
I don't know how to fix it so that the behavior remains the same.
Does anyone have any ideas how to fix this? Thanks.