[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers

Using the optional “+” sign together with mapped type modifiers, we can create more explicit and readable type declarations. We can also use the “-” (minus) sign to remove optional declarations from properties.

For example, we have an interface:

interface IPet {
    name: string;
    age: number;
    favoritePark?: string
}

There is two required props and one favoriatePark as optional prop.

From TypeScirpt 2.8, we are able to gereate a new interface based on existing one, and add or remove props:

For example we want to remove all the optional props, we can use ‘-‘:

interface IPetRequired {
  [K in keyof IPET]-?: IPet[K]
}

‘-‘: remove

‘?‘: optional

‘-?‘: remove optional

We can also use ‘+‘ to indicate what we have added:

type ReadonlyPet = {
    +readonly [K in keyof IPet]?: IPet[K]
}

Here we added readonly type.

原文地址:https://www.cnblogs.com/Answer1215/p/10284223.html

时间: 2024-11-05 12:18:30

[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers的相关文章

[TypeScript] Union Types and Type Aliases in TypeScript

Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an array. This lesson will show us how to assign more than 1 type to a variable with Typescript union types and type aliases. type types = string | boole

vue element-ui typescript tree报错 === Property 'getCheckedNodes' does not exist on type 'Element | Element[] | Vue | Vue[]'.

import { Tree } from 'element-ui' const ref = <Tree>this.$refs.tree ref.getCheckedNodes() vue element-ui typescript tree报错 === Property 'getCheckedNodes' does not exist on type 'Element | Element[] | Vue | Vue[]'. 原文地址:https://www.cnblogs.com/cynthi

[TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript

This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types differ from nullable types. It also illustrates how you can write safer code by being explicit about null and undefined in the type system. First of all

Bean named &#39;...&#39; is expected to be of type [...] but was actually of type [com.sun.proxy.$Proxy7解决方法

报错 三月 07, 2017 8:09:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org[email protected]41cf53f9: startup date [Tue Mar 07 20:09:52 CST 2017]; root of context hierarchy三月 07, 2017 8:09:52 下午 org.s

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

刚刚有在程序中,传递一个空值至MS SQL Server数据库,这个值的数据类型为DATETIME执行时,它却发生了如标题提示的异常:The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. 跟踪一下,当遇上一个空值时,它并没有真正是给一个空值给数据库,而是Datetime的最小值"1/1/0001 12:00:00 AM" 在两个文本框都是空值时,跟

customerized convert from field type to DB field&#39;s type

@LastModifiedDate @Convert(converter = LocalDateTime2TimestampConverter.class) @Slf4j public class LocalDateTime2TimestampConverter implements AttributeConverter<LocalDateTime, Timestamp> { @Override public Timestamp convertToDatabaseColumn(LocalDat

Jpa自定义查询报错(Failed to convert from type [java.lang.Object[]] to type)

Jpa自定义查询报错 问题背景 今天遇到一个奇怪的报错"Failed to convert from type [java.lang.Object[]] to type",这个报错,百度上也是很少的,恰恰是这样的问题,引起我了解决的欲望.先看看报错: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [o

报错The &quot;chunk&quot; argument must be one of type string or Buffer. Received type object

报错内容: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object at ServerResponse.end (_http_outgoing.js:690:13) 原因: response.end()方法接收的参数类型只能是字符串或Buffer, 传入的是object 解决: 找到代码出错行,重写end方法,如:r

[TypeScript] Create random integers in a given range

Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int between * @param start inclusive * @param before exclusive */ export function randomInt(start: number, before: number) { return start + Math.floor(Math.rand