人工智能意念控制打字
目的 (The Aim)
I want the objects I use in my TypeScript code to let me set multiple properties on them in one go. My objects must have a method setProps
such that when given a properly typed map of properties, the method sets the corresponding properties on the objects.
我希望在TypeScript代码中使用的对象能够让我一次性设置多个属性。 我的对象必须具有setProps
方法,以便在给定正确类型的属性映射时,该方法将在对象上设置相应的属性。
该方法 (The Approach)
I have a base class, say BaseObject
, that has the publicsetProps
method. setProps
must take parameters that are compatible with the instance of a class derived from BaseClass. This is achieved with generics. The method will look like the following piece of code:
我有一个基类,例如BaseObject
,它具有公共setProps
方法。 setProps
必须使用与从BaseClass派生的类的实例兼容的参数。 这是通过泛型实现的。 该方法将类似于以下代码:
I constrain the type parameter T
to represent a type that inherits from BaseObject
by using the syntax <T extends BaseObject>
.
我使用语法<T extends BaseObject>
约束类型参数T
表示从BaseObject
继承的类型。
Now, I want to assign the properties of the passed parameter to the object on which setProps
was called. To achieve this, I proceed as shown in the following excerpt:
现在,我想将传递的参数的属性分配给调用setProps
的对象。 为此,请按照以下摘录所示进行操作:
The for in
loop gets all the keys in values
and for each such property, if its value is not null, assigns it to the same property on the calling object.
for in
循环获取values
所有键,并且对于每个此类属性,如果其值不为null,则将其分配给调用对象上的相同属性。
Now I want to be sure that methods are not taken into account in the for in
loop. For that reason, I will use mapped types and the npm package utility-types which, as it name indicates, provides TypeScript utility types. I will useNonFunctionKeys
, provided by utility-types
, to define the following:
现在,我要确保在for in
循环中不考虑方法。 因此,我将使用映射类型和npm软件包实用程序类型 ,顾名思义,它们提供TypeScript实用程序类型。 我将使用utility-types
提供的NonFunctionKeys
来定义以下内容:
Above, we used type level programming with the expression {[P in NonFunctionKeys<T>]:T[P]}
to simply say thatOnlyProperties
, applied to the type parameter T, is a type made only of the non-method keys of T. If you haven’t done so yet, I strongly suggest you get acquainted to TypeScript mapped types. They are truly useful.
上面,我们使用带有表达式{[P in NonFunctionKeys<T>]:T[P]}
类型级别编程来简单地说,应用于类型参数T的OnlyProperties
是仅由非方法键构成的类型。 T.如果您还没有这样做,我强烈建议您熟悉TypeScript 映射类型 。 它们确实有用。
Our newly created type will be used in setProps
as such:
我们新创建的类型将在setProps
使用,如下所示:
By saying I want bulk setting of properties, I also imply partial setting of the said properties. To achieve this, I will enlist the help of the Partial utility type provided by Typescript. For any type passed to it, it makes all properties of that object optional. Using Partial,
I will be able to write the following type:
通过说我想批量设置属性,我也暗示了所述属性的部分设置。 为此,我将寻求Typescript提供的Partial实用程序类型的帮助。 对于传递给它的任何类型,它都使该对象的所有属性成为可选属性。 使用Partial,
我将能够编写以下类型:
Hence, we now have setProps
defined as:
因此,我们现在将setProps
定义为:
Here is an example usage of setProps
:
这是setProps
用法setProps
:
Please note that the parameter to setProps
will fail compiler checks if it is not of type O because I supplied the type parameter O.
请注意,因为我提供了类型参数O,所以setProps
的参数将不能通过编译器检查,如果它不是O类型。
奖金 (Bonus)
Why stop here when we can get a bulk property assignment constructor for almost free? On any class you derive from BaseObject
, you can use setProps
to gain the bulk property assignment constructor like so:
当我们几乎免费获得批量属性分配构造函数时,为什么要在这里停下来? 在从BaseObject
派生的任何类上,都可以使用setProps
来获取批量属性分配构造函数,如下所示:
结论 (Conclusion)
With generics and mapped types, we can implement type-safe, bulk assignment on our TypeScript objects. This is particularly useful when the number of properties on an object grows large and writing all the assignments becomes tedious.
使用泛型和映射类型,我们可以在TypeScript对象上实现类型安全的批量分配 。 当对象上的属性数量变大并且编写所有分配变得乏味时,这特别有用。
The code for this article can be found at https://github.com/kanian/setProps .
可以在 https://github.com/kanian/setProps中 找到本文的代码 。
You can reach me at patrick.assoa.adou[at]gmail.com
您可以通过patrick.assoa.adou [at] gmail.com与我联系。
翻译自: https://medium.com/@patrick.assoa.adou/smart-typescript-bulk-property-assignment-52162c1031da
人工智能意念控制打字