We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partial 可以快速把某个接口类型中定义的所有属性变成可选的。
举个栗子:
interface ApiKey { id: number; name: string; } const dataType1: ApiKey = { id: 1, name: 'static' } const dataType2: ApiKey = { name: 'json' }
这段代码会在编译报错:
error TS2741: Property 'id' is missing in type '{ name: string; }' but required in type 'ApiKey'.Key'.
因为dataType2的类型是ApiKey,ApiKey中id和name都是必选的,这导致编译报错。假如ApiKey中的参数是可选的,那么这个问题就会不复存在,而Partial的作用就在于此,它可以帮助我们把ApiKey中的所有属性都变成可选的。
dataType2
id
name
ApiKey
Partial
我们永Partial来重写一下这个栗子:
interface ApiKey { id: number; name: string; } const dataType1: ApiKey = { id: 1, name: 'static' } const dataType2: Partial<ApiKey> = { name: 'json' }
这个时候在运行,就不会报错了。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Partial
Partial 可以快速把某个接口类型中定义的所有属性变成可选的。
举个栗子:
这段代码会在编译报错:
因为
dataType2
的类型是ApiKey,ApiKey中id
和name
都是必选的,这导致编译报错。假如ApiKey
中的参数是可选的,那么这个问题就会不复存在,而Partial
的作用就在于此,它可以帮助我们把ApiKey
中的所有属性都变成可选的。我们永
Partial
来重写一下这个栗子:这个时候在运行,就不会报错了。
The text was updated successfully, but these errors were encountered: