@himanoa/ntd
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

@himanoa/ntd

ntd is TypeScript library that helps implement the new type pattern.

whici is used to treat two types with same structure as different types in TypeScript.

Installation

For npm users:

npm install @himanoa/ntd

Usages

// Type definition(user.ts)

import {
  makeValidator,
  makeValidationSmartConstructor,
  Ok,
  Err
} from "@himanoa/ntd";

declare const UrlStringSym: unique symbol;

const urlVaidator: Validator<string, { kind: "invalidFormat" }> = makeVaidator<string, { kind: 'invalidFormat' }>((ok, err) => (value) => {
  try {
    new URL(value)
    return ok(value)
  } catch {
    return err({kind: 'invalidFormat'})
  }
})

export const makeUrlString = makeValidationSmartConstructor<
  string,
  typeof UserNameSym
>()(urlValidator);

export type UrlString = FromValidationSmartConstructor<
  typeof makeUrlString
>;

// Use defined types

const validUrlStringResult = makeUrlString("https://example.com/");
const invalidUrlStringResult = makeUrlString("example!!!"); // 

if (validUrlStringResult.ok) {
  validUrlStringResult.v // https://example.com/
}

if (!invalidUrlStringResult.ok) {
  invalidUrlStringResult.e // { kind: 'invalidFormat' }
}

Contributing

Contributions are welcome! Please feel free to open an issue or pull request.

License

This library is licensed under the MIT license. See the LICENSE file for more information.

Readme

Keywords

none

Package Sidebar

Install

npm i @himanoa/ntd

Weekly Downloads

0

Version

0.0.4

License

MIT

Unpacked Size

6.05 kB

Total Files

8

Last publish

Collaborators

  • himanoa