https://github.com/zxch3n/typed-string
Use JSON.parse & JSON.stringify safely with TypeScript
https://github.com/zxch3n/typed-string
frontend javascript json typescript web
Last synced: 3 months ago
JSON representation
Use JSON.parse & JSON.stringify safely with TypeScript
- Host: GitHub
- URL: https://github.com/zxch3n/typed-string
- Owner: zxch3n
- Created: 2021-03-11T09:41:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-11T10:06:03.000Z (over 5 years ago)
- Last Synced: 2025-11-27T12:02:19.938Z (7 months ago)
- Topics: frontend, javascript, json, typescript, web
- Language: TypeScript
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Typed String
> Use JSON.parse & JSON.stringify safely with TypeScript 🥳
## Usage
**Install**
```
yarn add -D typed-string
```
**Use in TypeScript**
```typescript
import {StringifyFrom} from 'typed-string'
/**
* Get special string type `StringifyFrom` 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` type. StringifyFrom is just string type:
```typescript
type StringifyFrom = string & {
toString: () => StringifyFrom;
};
```