Skip to content

Home > sculp-js > is

is() function

判断值是否为指定类型(带类型守卫)

Signature:

typescript
export declare function is<T extends TypeTag>(value: unknown, type: T): value is TypeMapping[T];

Parameters

Parameter

Type

Description

value

unknown

要检查的值

type

T

期望的类型

Returns:

value is TypeMapping[T]

{boolean} 如果值是指定类型则返回 true

Example 1

is(value, 'String') is(value, 'Array') is(value, 'Date')

Example 2

// 使用类型守卫 if (is(value, 'String')) { // value 在这里被推断为 string 类型 console.log(value.toUpperCase()); }

Released under the MIT License.