/
githubmirror
/
tldraw
Обзор
Документация
Войти
/
githubmirror
/
tldraw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/utils/src/lib/stringEnum.ts
27 строк
868 B
Steve Ruiz
Automate docs, sort of (#6722)
06 окт 2025, 12:57
Не верифицирован
06 окт 2025, 12:57
4197e7a
Код
Авторство
О чём код?
/** * Creates an enum-like object from string values where each key maps to itself. * Useful for creating string constant objects with type safety and autocompletion. * @param values - The string values to create the enum from. * @returns An object where each provided string is both the key and value. * @example * ```ts * const Colors = stringEnum('red', 'green', 'blue') * // Results in: { red: 'red', green: 'green', blue: 'blue' } * * // Type-safe usage * function setColor(color: keyof typeof Colors) { * console.log(`Setting color to ${Colors[color]}`) * } * * setColor('red') // ✓ Valid * setColor('yellow') // ✗ TypeScript error * ``` * @internal */ export function stringEnum<T extends string>(...values: T[]): { [K in T]: K } { const obj = {} as { [K in T]: K } for (const value of values) { obj[value] = value } return obj }