'Why in a generic function, if the param is or not is optional, will cause the same call operation to inference different type?
For example the following code:
function a<T extends string | undefined>(value?: T): T {
return value as T;
}
function b<T extends string | undefined>(value: T): T {
return value;
}
const val = 'value' as string | undefined;
const i = a(val);
const j = b(val);
ant it's type is:
declare function a<T extends string | undefined>(value?: T): T;
declare function b<T extends string | undefined>(value: T): T;
declare const val: string | undefined;
declare const i: string;
declare const j: string | undefined;
so, why i's type is string, and j's type is string | undefined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
