1.0.4 • Published 3 years ago

typed-string v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Typed String

Use JSON.parse & JSON.stringify safely with TypeScript 🥳

Usage

Install

yarn add -D typed-string

Use in TypeScript

import {StringifyFrom} from 'typed-string'

/**
 * Get special string type `StringifyFrom<T>` after JSON.stringify.
 */

// const a: StringifyFrom<{ a: number; }>
const a = JSON.stringify({a: 123}) 

/**
 *  Infer parsed object type
 */

// const b: {a: number}
const b = JSON.parse(a) 

/**
 *  It will fallback to any if no types are matched
 */

// const c: any
const c = JSON.parse('"normal string"'); 

// const d: {v: number}
const d = JSON.parse("{v: 123}" as StringifyFrom<{v: number}>); 

It exposes StringifyFrom<T> type. StringifyFrom is just string type:

type StringifyFrom<T> = string & {
    toString: () => StringifyFrom<T>;
};