Summary: The issue you are facing is due to automatic semicolon insertion in conditional types. Simply remove the line break before extends
and the problem will be resolved:
type Equal<A, B> =
(<T>() => T extends A ? true : false) extends // this is fine
(<T>() => T extends B ? true : false)
? true
: false;
JavaScript, and consequently TypeScript, has a quirky behavior where it may sometimes insert semicolons at the end of lines for you if they are missing in statements. This is known as automatic semicolon insertion.
Sometimes, adding line breaks where whitespace typically goes is not allowed because the language might insert semicolons there. What a helpful feature, right?
The issue with conditional types like AAA extends BBB ? CCC : DDD
arises because AAA
is a valid type and extends
is recognized as a valid property name:
interface Foo {
a: string
extends: number; // <-- this is okay
}
Therefore, the TypeScript compiler detects a potential automatic semicolon insertion risk before the extends
in a conditional type. Avoid placing a line break there. Refer to microsoft/TypeScript#21649 for more details.
If you eliminate the line break before extends
, your code will compile correctly.
It's worth noting that the line break was not originally part of the source document you copied this type from; you or your code formatter must have inserted it inadvertently.
Playground link to code