1.0.0 • Published 9 months ago

define-exact v1.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 months ago

define-exact

npm

A TypeScript utility function for defining exact subtypes of the specified type, preserving specific property keys and literal types.

Installation

npm install define-exact

Usage

The defineExact function helps in creating objects with exact types, allowing TypeScript to infer function argument types without explicit annotations. It's particularly useful when you want the compiler to retain the most specific type information possible.

import defineExact from 'define-exact';

// A base type
type Foo = {
  [key: string]: (arg: number) => string;
};

// typeof exactFoo: {exactKey: (arg: number) => string}
const exactFoo = defineExact<Foo>()({
  exactKey: arg => arg.toString(),
});

// typeof veryExactFoo: {exactKey: (arg: number) => `literal ${number}`}
const veryExactFoo = defineExact<typeof exactFoo>()({
  exactKey: arg => `literal ${arg}` as const,
});

// A possible way to "unexact"
const foo: Foo = veryExactFoo;
1.0.0

9 months ago