基本类型
string(字符串)
关键字
以下关键字可以在任何定义中引用,例如:
| Alias | Description |
|---|---|
| string | a string |
| string.alpha | only letters |
| string.alphanumeric | only letters and digits 0-9 |
| string.base64 | base64-encoded |
| string.base64.url | base64url-encoded |
| string.capitalize | a morph from a string to capitalized |
| string.capitalize.preformatted | capitalized |
| string.creditCard | a credit card number and a credit card number |
| string.date | a string and a parsable date |
| string.date.epoch | an integer string representing a safe Unix timestamp |
| string.date.epoch.parse | a morph from an integer string representing a safe Unix timestamp to a Date |
| string.date.iso | an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date |
| string.date.iso.parse | a morph from an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date to a Date |
| string.date.parse | a morph from a string and a parsable date to a Date |
| string.digits | only digits 0-9 |
| string.email | an email address |
| string.hex | hex characters only |
| string.integer | a well-formed integer string |
| string.integer.parse | a morph from a well-formed integer string to an integer |
| string.ip | an IP address |
| string.ip.v4 | an IPv4 address |
| string.ip.v6 | an IPv6 address |
| string.json | a JSON string |
| string.json.parse | safe JSON string parser |
| string.lower | a morph from a string to only lowercase letters |
| string.lower.preformatted | only lowercase letters |
| string.normalize | a morph from a string to NFC-normalized unicode |
| string.normalize.NFC | a morph from a string to NFC-normalized unicode |
| string.normalize.NFC.preformatted | NFC-normalized unicode |
| string.normalize.NFD | a morph from a string to NFD-normalized unicode |
| string.normalize.NFD.preformatted | NFD-normalized unicode |
| string.normalize.NFKC | a morph from a string to NFKC-normalized unicode |
| string.normalize.NFKC.preformatted | NFKC-normalized unicode |
| string.normalize.NFKD | a morph from a string to NFKD-normalized unicode |
| string.normalize.NFKD.preformatted | NFKD-normalized unicode |
| string.numeric | a well-formed numeric string |
| string.numeric.parse | a morph from a well-formed numeric string to a number |
| string.regex | a string and a regex pattern |
| string.semver | a semantic version (see https://semver.org/) |
| string.trim | a morph from a string to trimmed |
| string.trim.preformatted | trimmed |
| string.upper | a morph from a string to only uppercase letters |
| string.upper.preformatted | only uppercase letters |
| string.url | a string and a URL string |
| string.url.parse | a morph from a string and a URL string to a URL instance |
| string.uuid | a UUID |
| string.uuid.v1 | a UUIDv1 |
| string.uuid.v2 | a UUIDv2 |
| string.uuid.v3 | a UUIDv3 |
| string.uuid.v4 | a UUIDv4 |
| string.uuid.v5 | a UUIDv5 |
| string.uuid.v6 | a UUIDv6 |
| string.uuid.v7 | a UUIDv7 |
| string.uuid.v8 | a UUIDv8 |
字面量
模式
正则表达式字面量指定了一个输入字符串必须匹配的、非锚定的正则表达式。
它们可以是字符串内嵌的,也可以直接引用一个 RegExp 实例。
长度
使用包含性或排除性的最小或最大长度来约束字符串。
范围表达式允许您同时指定最小和最大长度,并使用相同的语法来表示排除性。
number(数字)
关键字
以下关键字可以在任何定义中引用,例如:
| Alias | Description |
|---|---|
| number | a number |
| number.Infinity | Infinity |
| number.NaN | NaN |
| number.NegativeInfinity | -Infinity |
| number.epoch | an integer representing a safe Unix timestamp |
| number.integer | an integer |
| number.safe | at least -9007199254740991 and at most 9007199254740991 |
字面量
范围
使用包含性或排除性的最小值或最大值来约束数字。
范围表达式允许您同时指定最小值和最大值,并使用相同的语法来表示排除性。
除数
将 number 约束为指定整数的倍数。
bigint(大整数)
要允许任何 bigint 值,请使用 "bigint" 关键字。
字面量
要在类型中要求精确的 bigint 值,您可以在字符串内嵌的数字字面量后添加后缀 n,使其成为 bigint。
您也可以使用单元表达式来定义 bigint 字面量。
symbol(符号)
要允许任何 symbol 值,请使用 "symbol" 关键字。
要在定义中引用特定的符号,请使用单元表达式。
定义符号属性(如 { [mySymbol]: "string" })不需要特殊语法。关于如何将符号键与可选性等其他语法结合使用的更多信息和示例,请参阅属性。
boolean(布尔值)
要允许 true 或 false,请使用 "boolean" 关键字。
字面量
要要求特定的布尔值,请使用相应的字面量。
null
"null" 关键字可用于允许精确值 null,通常作为联合类型的一部分。
undefined
"undefined" 关键字可用于允许精确值 undefined,通常作为联合类型的一部分。
允许 undefined 作为值并不会使键变为可选!
在 TypeScript 中,一个允许 undefined 的必需属性仍然必须存在,类型才能得到满足。
在 ArkType 中也是如此。