0
votes

Is there any way Typescript can infer b to also be of type U extends Foo ? string : number? If a is of type U extends Foo ? string : number then in the true branch adding a number to string would result in string. In the false branch, adding a number to number would end up being a number. Thus I'd expect the type of b to also be inferred to U extends Foo ? string : number. Or is it a wrong assumption?

When I run the snippet below I get the following error:

Operator '+' cannot be applied to types 'U extends Foo ? string : number' and 'number'.(2365)

interface Foo {
  propA: boolean;
  propB: boolean;
}

declare function f<T>(x: T): T extends Foo ? string : number;

function foo<U>(x: U, d: number) {
  const a = f(x);
  const b = a + 2;
}

Playground Link

2
The error is there to tell you that you shouldn't add a number to a string. - Roberto Zvjerković
You can add a number to a string. That results in a string. Typescript has no issues with that: Playground Link - Lukasz Prus
Not with unions: function add(s: string | number) { return s + 1; } // Error - Roberto Zvjerković
a is of a conditional type, not a union type - Lukasz Prus

2 Answers

0
votes

The issue is related to strict behavior of + operator in TS. TS will never allow for using + in situation where the result can be more than primary type string or primary type number. Function foo is using + in such a way, that the result type is union string | number, as the function is polymorphic and if the argument x extends Foo then we will get string if not number.

Conditional type presented at level of f is not evaluated to the final type in foo, is not, as we have type variable U at this level. It would be evaluated to specific type if type would be given explicitly like in below examples:

// types provided directly
const x =  f(1) // number
const y = f({propA: true, propB: false}) // string

function inside<U>(a:U) {
  const x =  f(a) // T extends Foo ? string : number // not evaluated
  return x + 1; // error
}

function insideButExactBranch<U extends Foo>(a:U) {
  const x =  f(a)
  return x + 1; // ok no error
}

In foo we still work with unknown U, that is why result of f is considered as any possible, that means - string | number. And if so the + cannot be compiled.

In other words - TS cannot evaluate the result of f function, therefor it takes the all possible types of return from the conditional type, what means union string | number. More about why using + is restricted in TS here - Why TypeScript restricts + operator

-1
votes

Typescript reports an error here not because the Javascript code will fail at runtime, but because according to the type annotations this code sometimes does string concatenation, and other times does arithmetic, and this usually means the programmer has made a mistake.

When you write + you almost always intend either that it means string concatenation, or you intend that it means arithmetic, but you rarely intend that it could mean both. So, this is an error to warn about code which is probably wrong.

If you really want a + that sometimes does string concatenation and sometimes does arithmetic, you can add a @ts-ignore comment before that line. This is similar to @SuppressWarnings in Java; you're telling the compiler not to warn you about something which usually indicates a mistake, because in this case it's not a mistake and you don't need the compiler to warn you about it.

function foo<U>(x: U, d: number) {
  const a = f(x);
  // @ts-ignore
  const b = a + 2;
}