简体   繁体   English

如何在TypeScript中定义Partials数组?

[英]How do you define an array of Partials in TypeScript?

For instance, 例如,

type User = {
  first_name: string,
  last_name: string,
  notes: string
}

Which of the following would work? 以下哪项可行?

const users:Partial<User>[] = [{first_name: "Name"}]; // A

const users:Partial<User[]> = [{first_name: "Name"}]; // B

const users:[Partial<User>] = [{first_name: "Name"}]; // C

Weirdly, all of these appear to be valid TypeScript at compile time. 奇怪的是,所有这些在编译时似乎都是有效的TypeScript。

Thanks in advance. 提前致谢。

The proper way is 正确的方法是

const users:Partial<User>[] = [{first_name: "Name"}];

Partial<User[]> means that it has some of Array properties . Partial<User[]>表示它具有一些Array属性

[Partial<User>] means that it's an array of partial User with 1 element. [Partial<User>]表示它是具有1个元素的部分User的数组。

Weirdly, all of these appear to be valid TypeScript at compile time 奇怪的是,所有这些在编译时似乎都是有效的TypeScript

The syntax is valid but types aren't. 语法有效但类型不是。 It's expected that Partial<User[]> causes an error because types are incompatible. 预计Partial<User[]>会导致错误,因为类型不兼容。

Option A gives you an array containing Partials: 选项A为您提供包含部分的数组:

const users:Partial<User>[] = [{first_name: "Name"}]; // A

If it helps you understand, think about how you would define an array of Partials if the type weren't generic. 如果它有助于您理解,请考虑如果类型不是通用的,您将如何定义Partials数组。 That would be Partial[] . 那将是Partial[]

If you remove the generic type from option B, you'd get a single Partial instead of an array. 如果从选项B中删除泛型类型,则会获得单个“ Partial而不是数组。

Option C is a tuple rather than an array. 选项C是元组而不是数组。

Even though all turn out to be similar in complied JavaScript, there is huge difference in all three. 尽管在编译的JavaScript中结果都相似,但这三者之间存在巨大差异。

const users:Partial<User>[] = [{first_name: "Name"}];

This is a valid array of Partial. 这是一个有效的Partial数组。 By appending [] at end, you are specifying that the length is unknown and can be any. 通过在末尾添加[],您指定长度未知且可以是任何长度。 So you can have as many elements of User in this array as you want. 因此,您可以根据需要在此数组中包含尽可能多的User元素。 Remember this as it will help with next example. 记住这一点,因为它将有助于下一个例子。

In the second example, you are trying to do the same but it is different and invalid. 在第二个示例中,您尝试执行相同操作但它不同且无效。

const users:Partial<User[]> = [{first_name: "Name"}];

You are trying to turn an array of User to Partial. 您正在尝试将用户数组转换为部分。 So, User is not yet partial but you are trying to turn array into partial. 所以,用户还不是局部的,但是你试图将数组变成局部的。 You get an error: 你收到一个错误:

Type '{ first_name: string; }[]' is not assignable to type 'Partial<User[]>'.
  Types of property 'pop' are incompatible.
    Type '() => { first_name: string; }' is not assignable to type '() => User'.
      Type '{ first_name: string; }' is not assignable to type 'User'.
        Property 'last_name' is missing in type '{ first_name: string; }'.

In your 3rd example: 在你的第三个例子中:

const users: [Partial<User>] = [{ first_name: "Name" }]; // C

You are doing the same as in your first example and it is valid. 您正在执行与第一个示例相同的操作,它是有效的。 Creating an array of Partial but there is a difference. 创建一个Partial数组但是有区别。 Here, you can have only one element in this array. 在这里,您只能在此数组中包含一个元素。 If you try to add another element, you get an error because with [Partial<User>] also specifies that the length of array will be exactly 1. 如果您尝试添加另一个元素,则会出现错误,因为[Partial<User>]还指定数组的长度恰好为1。

Let us see, what is the error in following: 让我们看看,下面的错误是什么:

const users: [Partial<User>] =    [{ first_name: "Name" }, { first_name: "Name" }];

Here the error is: 这里的错误是:

    Type '[{ first_name: string; }, { first_name: string; }]' is not assignable to type '[Partial<User>]'.
  Types of property 'length' are incompatible.
    Type '2' is not assignable to type '1'.

Strangely, the TypeScript does compile all of your examples to similar array, but it does give you the errors in its online compiler. 奇怪的是,TypeScript确实将所有示例编译为类似的数组,但它确实在其在线编译器中提供了错误。

There is an online tool of TypeScript to play and see your code turning into JavaScript. 有一个TypeScript的在线工具可以播放并看到你的代码变成了JavaScript。 https://www.typescriptlang.org/play/ The good thing is that you can actually see the problem with your code in the typescript code section. https://www.typescriptlang.org/play/好的是你可以在typescript代码部分看到代码的问题。 Any issue with code will have a red underline and hover over it to see the details. 代码的任何问题都会有一个红色下划线并将鼠标悬停在它上面以查看详细信息。

TypeScript在线播放显示错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM