12 часов назад
История
README.md
@cutticat/types
TypeScript type library.
Contents
Overview
This library provides a set of useful TypeScript types for working with strings, objects, nullability, mutability, and HTTP. The types are optimized for strict TypeScript projects and provide compile-time type safety.
Installation
pnpm i @cutticat/types
Quick start
import type {
ToCamelCase,
ToSnakeCase,
Join,
NestedRecord,
DeepReadonly,
FullyUndefinable,
PathParams,
} from "@cutticat/types"
// String transformations
type CamelCase = ToCamelCase<"hello-world"> // "helloWorld"
type SnakeCase = ToSnakeCase<"helloWorld"> // "hello_world"
type Csv = Join<["red", "green", "blue"]> // "red,green,blue"
// Working with objects
type Nested = NestedRecord<["user", "address"], { city: string }>
// { user: { address: { city: string } } }
type ReadonlyConfig = DeepReadonly<{ name: string; age: number }>
type OptionalConfig = FullyUndefinable<{ name: string; age: number }>
// HTTP path parameters
type Params = PathParams<"/users/:id/posts/:postId"> // { id: string; postId: string }
API
Strings
Case conversion:
ToCamelCase<S>— converts to camelCaseToSnakeCase<S>— converts to snake_caseToPascalCase<S>— converts to PascalCaseToKebabCase<S>— converts to kebab-case
Prefixes and suffixes:
RemovePrefix<T, Prefix>— removes a prefixRemoveSuffix<T, Suffix>— removes a suffix
Utilities:
Join<Parts, Delimiter = ",">— joins a tuple of strings or numbers with a delimiterSplit<S, Delimiter = ",">— splits a string into a tuple of partsReplaceAll<S, From, To>— replaces all occurrences of a substring
Objects
NestedRecord<Path, Value>— nested object whose keys follow a tuple pathProperty<T, Path>— value type at a path in an objectPropertyPath— path of keys (Key[])PrefixKeys<T, Prefix>— adds a prefix to all keysKeysStartingWith<T, Prefix>— keys starting with a prefixKey— any valid object key (string | number | symbol)OwnKey— key type fromReflect.ownKeys(obj)(string | symbol)KeyToOwn<T>— convertsKeytoOwnKey(number→string)CleanOmit<T, K>—Omitwithout thestringkey artifact on objects with an index signatureExcludeFromProperties<T, E>/DeepExcludeFromProperties<T, E>— excludes values from propertiesIsPlainObject<T>— checks whether a value is a plain object
Nullability (nulls)
DeepRequired<T>— all properties are requiredDeepPartial<T>— all properties are optionalDeepNonNullable<T>— recursively removes null and undefined (mirror ofDeepNullish<T>)DeepNonNullableRequired<T>— removes null/undefined and makes all properties requiredNullish<T>/DeepNullish<T>— nullable properties (null | undefined)Undefinable<T>/DeepUndefinable<T>— properties allow undefinedFullyUndefinable<T>/DeepFullyUndefinable<T>— all properties are optional and allow undefinedIsOptional<T, K>— propertyKinTis declared optionalHasUndefined<T>—undefinedis part of the value typeCopyOptionalAndUndefined<Source, Target, Keys?>— values fromTargetby key, copying optionality andundefinedfromSource
Mutability
DeepReadonly<T, LocalExceptions?>— recursively makes all properties readonlyDeepWritable<T, LocalExceptions?>— recursively removes readonlyReadonlyDate— Date without mutation methodsImmutable,DeepReadonlyExceptions— exception mechanism for DeepReadonly/DeepWritable
HTTP
HttpMethod— union of all HTTP methodsHttpStatusCode— union of all HTTP status codesPathParams<Path>— extracts:paramparameters from a path string
Maybe (optional values)
MaybePromise<T>— value may bePromise<T>or synchronousTMaybeArray<T>— value may be an arrayT[]or a singleT
Common
UnionToIntersection<Union>— converts a union to an intersectionSimplify<T>— simplifies object type display (useful after intersections and conditional types)
Predicates
IsEqual<A, B>—truewhen the two types are identicalIsNever<T>/IsAny<T>/IsUnknown<T>—truefor those special typesIsTuple<T>—truefor a finite tuple,falsefor a general arrayExtends<A, B>—trueifAis assignable toB(non-distributive)Not<T>— boolean negationIf<Condition, Then, Else?>— picksThenorElse
Testing
Compile-time asserts for tsc --noEmit tests:
Assert<T>—Tmust betrueAssertAll<T>— every element of a tuple must betrueAssertEqual<Actual, Expected>— types must be identicalAssertNotEqual<Actual, Expected>— types must differAssertExtends<Actual, Expected>—Actualmust be assignable toExpected
Documentation
Detailed documentation:
Full API documentation is available in JSDoc comments. Use IDE autocomplete to browse it.
Requirements
- Node.js >= 22.18.0
- pnpm >= 10.17.0
- TypeScript >= 5.9.0
Описание
TypeScript utility types library
Конвейеры
9
успешных
1
с ошибкой