primtive.ts 733 B

12345678910111213141516171819202122232425262728293031323334
  1. export type JSPrimitive =
  2. | "undefined"
  3. | "object"
  4. | "boolean"
  5. | "number"
  6. | "bigint"
  7. | "string"
  8. | "symbol"
  9. | "function"
  10. export type TypeFromPrimitive<P extends JSPrimitive | undefined> =
  11. P extends "undefined"
  12. ? undefined
  13. : P extends "object"
  14. ? object | null // typeof null === "object"
  15. : P extends "boolean"
  16. ? boolean
  17. : P extends "number"
  18. ? number
  19. : P extends "bigint"
  20. ? BigInt
  21. : P extends "string"
  22. ? string
  23. : P extends "symbol"
  24. ? Symbol
  25. : P extends "function"
  26. ? Function
  27. : unknown
  28. export const isOfType =
  29. <T extends JSPrimitive>(type: T) =>
  30. (value: unknown): value is T =>
  31. // eslint-disable-next-line valid-typeof
  32. typeof value === type