Git Product home page Git Product logo

stronk's Introduction

image

stronk

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.

Motivation

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.

Installation

npm install stronk-ts

Usage

Creating Branded Types

import { type } from "stronk-ts";

const BrandedString = type.create("BrandedString", String);
const myString = BrandedString("hello world");

Runtime Type Checking

type.is(myString, BrandedString); // true
type.is("hello world", BrandedString); // false
type.is(myString, String); // narrows `myString` to type `string`

Specific Type Retrieval

type.of(myString); // "BrandedString"
type.of("hello world"); // "string"

Key Features

  • 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 standard typeof.

ESLint Plugin Rules

no-typeof

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");

no-typeof-comparison

Prevents typeof comparisons. Use type.is for comparisons.

// bad
if (typeof "hello world" === "string") {
    // ...
}

// good
if (type.is("hello world", String)) {
    // ...
}

Contributing

Contributions are welcome! Create an issue or open a pull request if there's something you'd like to be different.

Roadmap

The goal is to continuously evolve stronk, adding more features and capabilities to enhance the type system.

License

stronk is available under the MIT license.

stronk's People

Contributors

trvswgnr avatar

Stargazers

Devon 'fire' Adkisson avatar Mark McCulloh avatar David Skrundz avatar Dillon Mulroy avatar Greg avatar Elad Ben-Israel avatar Eiliya avatar  avatar  avatar

Watchers

 avatar

Forkers

samathingamajig

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.