import type { JSONParsed, JSONValue } from './types' describe('JSONParsed', () => { enum SampleEnum { Value1 = 'value1', Value2 = 'value2', } interface Meta { metadata: { href: string sampleEnum: SampleEnum } } interface SampleInterface { someMeta: Meta } type SampleType = { someMeta: Meta } describe('primitives', () => { it('should convert number type to number', () => { type Actual = JSONParsed type Expected = number expectTypeOf().toEqualTypeOf() }) it('should convert string type to string', () => { type Actual = JSONParsed type Expected = string expectTypeOf().toEqualTypeOf() }) it('should convert boolean type to boolean', () => { type Actual = JSONParsed type Expected = boolean expectTypeOf().toEqualTypeOf() }) it('should convert null type to null', () => { type Actual = JSONParsed type Expected = null expectTypeOf().toEqualTypeOf() }) }) describe('toJSON', () => { it('should convert { toJSON() => T } to T', () => { type Actual = JSONParsed<{ toJSON(): number }> type Expected = number expectTypeOf().toEqualTypeOf() }) it('toJSON is not called recursively', () => { type Actual = JSONParsed<{ toJSON(): { toJSON(): number } }> type Expected = {} expectTypeOf().toEqualTypeOf() }) it('should convert { a: { toJSON() => T } } to { a: T }', () => { type Actual = JSONParsed<{ a: { toJSON(): number } }> type Expected = { a: number } expectTypeOf().toEqualTypeOf() }) it('should convert invalid type with { toJSON() => T to T', () => { type Actual = JSONParsed type Expected = string expectTypeOf().toEqualTypeOf() }) }) describe('invalid types', () => { it('should convert undefined type to never', () => { type Actual = JSONParsed type Expected = never expectTypeOf().toEqualTypeOf() }) it('should convert symbol type to never', () => { type Actual = JSONParsed type Expected = never expectTypeOf().toEqualTypeOf() }) it('should convert function type to never', () => { type Actual = JSONParsed<() => void> type Expected = never expectTypeOf().toEqualTypeOf() }) it('should convert bigint type to never', () => { type Actual = JSONParsed type Expected = never expectTypeOf().toEqualTypeOf() }) }) describe('array', () => { it('should convert undefined[] type to null[]', () => { type Actual = JSONParsed type Expected = null[] expectTypeOf().toEqualTypeOf() }) it('should convert Function[] type to null[]', () => { type Actual = JSONParsed<(() => void)[]> type Expected = null[] expectTypeOf().toEqualTypeOf() }) it('should convert symbol[] type to null[]', () => { type Actual = JSONParsed type Expected = null[] expectTypeOf().toEqualTypeOf() }) it('should convert (T | undefined)[] type to JSONParsedT | null>[]', () => { type Actual = JSONParsed<(number | undefined)[]> type Expected = (number | null)[] expectTypeOf().toEqualTypeOf() }) it('should convert { key: readonly T[]} correctly', () => { type Actual = JSONParsed<{ key: readonly number[] }> type Expected = { key: readonly number[] } expectTypeOf().toEqualTypeOf() }) }) describe('tuple', () => { it('should convert [T, S] type to [T, S]', () => { type Actual = JSONParsed<[number, string]> type Expected = [number, string] expectTypeOf().toEqualTypeOf() }) it('should convert [T, undefined] type to [T, null]', () => { type Actual = JSONParsed<[number, undefined]> type Expected = [number, null] expectTypeOf().toEqualTypeOf() }) }) describe('object', () => { it('should omit keys with undefined value', () => { type Actual = JSONParsed<{ a: number; b: undefined }> type Expected = { a: number } expectTypeOf().toEqualTypeOf() }) it('should omit keys with symbol value', () => { type Actual = JSONParsed<{ a: number; b: symbol }> type Expected = { a: number } expectTypeOf().toEqualTypeOf() }) it('should omit keys with function value', () => { type Actual = JSONParsed<{ a: number; b: () => void }> type Expected = { a: number } expectTypeOf().toEqualTypeOf() }) it('should omit symbol keys', () => { type Actual = JSONParsed<{ a: number; [x: symbol]: number }> type Expected = { a: number } expectTypeOf().toEqualTypeOf() }) it('should convert T | undefined to T | undefined', () => { type Actual = JSONParsed<{ a: number; b: number | undefined }> type Expected = { a: number; b: number | undefined } expectTypeOf().toEqualTypeOf() }) it('should omit keys with invalid union', () => { type Actual = JSONParsed<{ a: number; b: undefined | symbol }> type Expected = { a: number } expectTypeOf().toEqualTypeOf() }) }) describe('unknown', () => { it('should convert unknown type to unknown', () => { type Actual = JSONParsed type Expected = JSONValue expectTypeOf().toEqualTypeOf() }) it('Should convert unknown value to JSONValue', () => { type Actual = JSONParsed<{ value: unknown }> type Expected = { value: JSONValue } expectTypeOf().toEqualTypeOf() }) }) describe('Set/Map', () => { it('should convert Set to empty object', () => { type Actual = JSONParsed> type Expected = {} expectTypeOf().toEqualTypeOf() }) it('should convert Map to empty object', () => { type Actual = JSONParsed> type Expected = {} expectTypeOf().toEqualTypeOf() }) }) it('Should parse a complex type', () => { const sample: JSONParsed = { someMeta: { metadata: { href: '', sampleEnum: SampleEnum.Value1, }, }, } expectTypeOf(sample).toEqualTypeOf() }) it('Should parse a complex interface', () => { const sample: JSONParsed = { someMeta: { metadata: { href: '', sampleEnum: SampleEnum.Value1, }, }, } expectTypeOf(sample).toEqualTypeOf() }) it('Should convert Date to string', () => { type Post = { datetime: Date } type Expected = { datetime: string } type Actual = JSONParsed expectTypeOf().toEqualTypeOf() }) it('Should convert bigint to never', () => { type Post = { num: bigint } type Expected = never type Actual = JSONParsed expectTypeOf().toEqualTypeOf() }) it('Should convert bigint when TError is provided', () => { type Post = { num: bigint } type Expected = { num: JSONValue } type Actual = JSONParsed expectTypeOf().toEqualTypeOf() }) it('Should convert bigint[] to never', () => { type Post = { nums: bigint[] } type Expected = never type Actual = JSONParsed expectTypeOf().toEqualTypeOf() }) it('Should convert bigint[] when TError is provided', () => { type Post = { num: bigint[] } type Expected = { num: JSONValue[] } type Actual = JSONParsed expectTypeOf().toEqualTypeOf() }) it('Should parse bigint with a toJSON function', () => { class SafeBigInt { unsafe = BigInt('42') toJSON() { return { unsafe: '42n', } } } type Actual = JSONParsed type Expected = { unsafe: string } expectTypeOf().toEqualTypeOf() }) })