I've seen this pattern and I don't understand what is going on.
interface Foo<T extends string | string[] = string | string[]> { }
I have an interface with a generic that extends three items. The second item contains an assignment where a string array is assigned to a string?
I haven't been able to find an answer on the forums because I find this problem hard to articulate.
Any help would be appreciated thanks.
CodePudding user response:
The expression is confusing, and it is not a Union of 3 items.
In fact it just says T extends string | string[]
, and the default type is string | string[]
if T is not specified.
So const foo: Foo
will be infer to const foo: Foo<string | string[]>
Here are some transformations to help you to understand what's happening.
1:
interface Foo<T extends (string | string[]) = (string | string[])> { }
2:
type StringOrArray = string | string[]
interface Foo<T extends StringOrArray = StringOrArray > { }