Skip to content

Primitive

JavaScript のすべてのプリミティブ値をまとめたユニオンです。プリミティブでないものはすべてオブジェクトです。

typescript
type Value = Primitive;

使用法

Primitive

どのプリミティブ値でも受け取るが、オブジェクトは受け取らない場所で使います。手で書くと bigintsymbol を書き忘れがちです。

typescript
import type { Primitive } from 'es-toolkit/types';

function isPrimitive(value: unknown): value is Primitive {
  return value === null || (typeof value !== 'object' && typeof value !== 'function');
}

const a: Primitive = 'text';
const b: Primitive = 1n;
const c: Primitive = Symbol('id');

// const d: Primitive = {}; // エラーです。オブジェクトはプリミティブではありません。

MIT ライセンスの下で配布されています。