function parse
parse<T>(thing: string): T | string

Safely parses a string to JSON or returns the string itself. This will never throw an error and internally called JSON.parse(thing)

Examples

Example 1

parse("Hello world!"); // "Hello world!"
parse<number>("0"); // 0
parse<User>("{\"name\":\"Bob\"}"); // { name: "Bob" } as User

Type Parameters

Parameters

thing: string

Return Type

T | string

Usage

import { parse } from ".";