Neverending Package Mountain

    typed-string
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.4 • Public • Published

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

    Install

    npm i typed-string

    DownloadsWeekly Downloads

    5

    Version

    1.0.4

    License

    ISC

    Unpacked Size

    1.78 kB

    Total Files

    3

    Last publish

    Collaborators

    • rem2018