@type-map-for/core
v0.1.1
Published
Compile-time field-mapping primitives — FieldTypeFor, MapType, and type utilities
Readme
@type-map-for/core
Core type utilities for the @type-map-for library ecosystem.
Installation
npm install @type-map-for/coreTypes
FieldTypeFor<T>
Maps TypeScript primitives to canonical field type strings:
import type { FieldTypeFor } from '@type-map-for/core';
type Examples = {
str: FieldTypeFor<string>; // 'string'
num: FieldTypeFor<number>; // 'int32' | 'float'
bool: FieldTypeFor<boolean>; // 'bool'
arr: FieldTypeFor<string[]>; // 'string[]'
obj: FieldTypeFor<{ x: number }>; // 'object'
};MapType<FT, M>
Translates field types via lookup map:
import type { MapType } from '@type-map-for/core';
type SqlMap = { string: 'text'; 'string[]': 'text[]'; bool: 'boolean' };
type SqlString = MapType<'string', SqlMap>; // 'text'
type SqlArray = MapType<'string[]', SqlMap>; // 'text[]'
type SqlBool = MapType<'bool', SqlMap>; // 'boolean'Utility Types
import type { IsArray, IsOptional, ItemType, RequiredKeys } from '@type-map-for/core';
type User = { name: string; email?: string; tags: string[] };
type NameIsArray = IsArray<User['name']>; // false
type TagsIsArray = IsArray<User['tags']>; // true
type EmailOptional = IsOptional<User['email']>; // true
type NameOptional = IsOptional<User['name']>; // false
type TagsItem = ItemType<User['tags']>; // string
type Required = RequiredKeys<User>; // 'name' | 'tags'Contributing
Issues and pull requests welcome at github.com/nathancanine/type-map-for.
License
MIT
