stronk
drastically improves the way you create and work with types. It introduces a way to create stronger, nominally typed systems, effectively enhancing JavaScript's typing capabilities. This library allows for runtime type checks, making it a powerful tool for developers who need precise, runtime-validatable branded types.
TypeScript's structural type system is fine, but it falls short in scenarios where nominal typing is needed. Types in TypeScript are not checkable at runtime. stronk
fills this gap by enabling developers to define types that are both nominally distinct and runtime-checkable, improving code safety and robustness.
npm install stronk-ts
import { type } from "stronk-ts";
const BrandedString = type.create("BrandedString", String);
const myString = BrandedString("hello world");
type.is(myString, BrandedString); // true
type.is("hello world", BrandedString); // false
type.is(myString, String); // narrows `myString` to type `string`
type.of(myString); // "BrandedString"
type.of("hello world"); // "string"
- Nominal Typing: Introduces nominal typing to TypeScript, making your types distinct beyond structural similarities.
- Runtime Type Checks:
type.is
allows you to check and narrow types at runtime, ensuring type safety beyond compile time. - Enhanced Type Identification:
type.of
returns specific types, providing more detailed type information than the standardtypeof
.
Disallows the use of typeof
for type checking. Use type.is
instead.
// bad
const stringType = typeof "hello world";
// good
const stringType = type.of("hello world");
Prevents typeof
comparisons. Use type.is
for comparisons.
// bad
if (typeof "hello world" === "string") {
// ...
}
// good
if (type.is("hello world", String)) {
// ...
}
Contributions are welcome! Create an issue or open a pull request if there's something you'd like to be different.
The goal is to continuously evolve stronk
, adding more features and capabilities to enhance the type system.
stronk
is available under the MIT license.