Typescript 通用语法 < P extends U |未定义=未定义>

Typescript Generic Syntax < P extends U | undefined = undefined >

我在浏览Next-auth.js类型定义时碰到了这个定义。

export declare function signIn<P extends RedirectableProviderType | undefined = undefined>(provider?: LiteralUnion<BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorizationParams): Promise<P extends RedirectableProviderType ? SignInResponse | undefined : undefined>;

定义的 undefined = undefined 部分是什么意思?

P extends RedirectableProviderType | undefined = undefined 表示在调用 signIn 时允许断言 RedirectableProviderTypeundefined 类型(例如 signIn<RedirectableProviderType>),但是当没有断言类型时提供 - P 将默认为 undefined.

也可以将其视为 P (extends RedirectableProviderType | undefined) = undefined