Deviopath › TypeScript

Classify JSON with unknown

Medium · Narrowing

Write jsonType(text: string): JsonType that parses text with JSON.parse and returns what kind of value it contains: "object", "array", "string", "number", "boolean" or "null". If parsing throws, return "invalid".

Store the parsed value as unknown and narrow it step by step. Careful: typeof null and typeof [] are both "object".

Next: Format a Table Row