Socket
Book a DemoInstallSign in
Socket

typed-string

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-string

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

latest
Source
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

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>;
};

Keywords

typescript

FAQs

Package last updated on 11 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts