0
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.

Ruslan
  • 101
  • 3
  • The root of the problem is the same as: [How to fix TS2322: "could be instantiated with a different subtype of constraint 'object'"?](https://stackoverflow.com/q/56505560/251311) – zerkms Oct 24 '22 at 22:45
  • And in short - that expression (effectively of a string type) is not assignable to the `S extends string ? \`${N}/${S}\` : N` because `N` is not necessary a string (but something that extends it). – zerkms Oct 24 '22 at 22:46

0 Answers0