https://github.com/danielwaltz/ts-define-constant
Type safe utility for defining static constants with automatic type narrowing and additional helper functions
https://github.com/danielwaltz/ts-define-constant
constant narrowing type-narrowing typescript utility
Last synced: 4 months ago
JSON representation
Type safe utility for defining static constants with automatic type narrowing and additional helper functions
- Host: GitHub
- URL: https://github.com/danielwaltz/ts-define-constant
- Owner: danielwaltz
- License: mit
- Created: 2023-02-08T04:20:53.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-08-19T14:40:51.000Z (11 months ago)
- Last Synced: 2025-10-25T16:46:17.369Z (8 months ago)
- Topics: constant, narrowing, type-narrowing, typescript, utility
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/ts-define-constant
- Size: 1.52 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# TS Define Constant [](https://npmjs.com/package/ts-define-constant)
Type safe utility for defining static constants with automatic type narrowing and additional helper functions. Zero dependencies.
## Install
```shell
# npm
npm i ts-define-constant
# yarn
yarn add ts-define-constant
# pnpm
pnpm i ts-define-constant
```
## Usage
Import and define your constants using the provided `defineConstant` function.
Supports passing in a plain object or an array of strings as the first argument.
```ts
import { defineConstant } from 'ts-define-constant';
const {
/* Plain object containing the constants */
object,
/* Array of the keys */
keys,
/* Array of the values */
values,
/* Function that returns the key of a given value */
getKey,
/* Function that returns the value of a given key */
getValue,
/* Function that returns true if the given value is a key of the constants */
isKey,
/* Function that returns true if the given value is a value of the constants */
isValue,
/* Function that returns true if the given value is equal to the USER constant */
isUser,
/* Function that returns true if the given value is equal to the ADMIN constant */
isAdmin,
} = defineConstant({ USER: 'USER', ADMIN: 'ADMIN' });
```
An example of what this looks like when multiple constants are defined and exported from a shared `constants.ts` file:
```ts
// constants.ts
import { defineConstant } from 'ts-define-constant';
export const {
object: USER_ROLES,
keys: USER_ROLE_KEYS,
values: USER_ROLE_VALUES,
getKey: getUserRoleKey,
getValue: getUserRoleValue,
isKey: isUserRoleKey,
isValue: isUserRoleValue,
isUser,
isAdmin,
} = defineConstant({ USER: 'USER', ADMIN: 'ADMIN' });
export const {
object: SORT_ORDERS,
keys: SORT_ORDER_KEYS,
values: SORT_ORDER_VALUES,
getKey: getSortOrderKey,
getValue: getSortOrderValue,
isKey: isSortOrderKey,
isValue: isSortOrderValue,
isAsc,
isDesc,
} = defineConstant({ ASC: 'ASC', DESC: 'DESC' });
```