Skip to content
Myr

types.test.ts

This file

Path

src/utils/types.test.ts

217 lines · 6.7 KiB · TypeScript

Last changed by

fix(types): handle readonly array correctly (#3172)m-shakabefore adoptionbb14b1e

History of this file

Take it

The walk stopped after 200 commits: lines older than 7ee829b point at it.

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 1–23why, one step back
1import type { Equal, Expect, JSONParsed } from './types'
23describe('JSONParsed', () => {4 enum SampleEnum {5 Value1 = 'value1',6 Value2 = 'value2',7 }89 interface Meta {10 metadata: {11 href: string12 sampleEnum: SampleEnum13 }14 }1516 interface SampleInterface {17 someMeta: Meta18 }1920 type SampleType = {21 someMeta: Meta22 }23

feat(types): improve JSONParsed (#3074)m-shakabefore adoption2a4f257

lines 24–103why, one step back
24 describe('primitives', () => {
25 it('should convert number type to number', () => {26 type Actual = JSONParsed<number>27 type Expected = number28 expectTypeOf<Actual>().toEqualTypeOf<Expected>()29 })30 it('should convert string type to string', () => {31 type Actual = JSONParsed<string>32 type Expected = string33 expectTypeOf<Actual>().toEqualTypeOf<Expected>()34 })35 it('should convert boolean type to boolean', () => {36 type Actual = JSONParsed<boolean>37 type Expected = boolean38 expectTypeOf<Actual>().toEqualTypeOf<Expected>()39 })40 it('should convert null type to null', () => {41 type Actual = JSONParsed<null>42 type Expected = null43 expectTypeOf<Actual>().toEqualTypeOf<Expected>()44 })45 })4647 describe('toJSON', () => {48 it('should convert { toJSON() => T } to T', () => {49 type Actual = JSONParsed<{ toJSON(): number }>50 type Expected = number51 expectTypeOf<Actual>().toEqualTypeOf<Expected>()52 })53 it('toJSON is not called recursively', () => {54 type Actual = JSONParsed<{ toJSON(): { toJSON(): number } }>55 type Expected = {}56 expectTypeOf<Actual>().toEqualTypeOf<Expected>()57 })58 it('should convert { a: { toJSON() => T } } to { a: T }', () => {59 type Actual = JSONParsed<{ a: { toJSON(): number } }>60 type Expected = { a: number }61 expectTypeOf<Actual>().toEqualTypeOf<Expected>()62 })63 })6465 describe('invalid types', () => {66 it('should convert undefined type to never', () => {67 type Actual = JSONParsed<undefined>68 type Expected = never69 expectTypeOf<Actual>().toEqualTypeOf<Expected>()70 })71 it('should convert symbol type to never', () => {72 type Actual = JSONParsed<symbol>73 type Expected = never74 expectTypeOf<Actual>().toEqualTypeOf<Expected>()75 })76 it('should convert function type to never', () => {77 type Actual = JSONParsed<() => void>78 type Expected = never79 expectTypeOf<Actual>().toEqualTypeOf<Expected>()80 })81 })8283 describe('array', () => {84 it('should convert undefined[] type to null[]', () => {85 type Actual = JSONParsed<undefined[]>86 type Expected = null[]87 expectTypeOf<Actual>().toEqualTypeOf<Expected>()88 })89 it('should convert Function[] type to null[]', () => {90 type Actual = JSONParsed<(() => void)[]>91 type Expected = null[]92 expectTypeOf<Actual>().toEqualTypeOf<Expected>()93 })94 it('should convert symbol[] type to null[]', () => {95 type Actual = JSONParsed<symbol[]>96 type Expected = null[]97 expectTypeOf<Actual>().toEqualTypeOf<Expected>()98 })99 it('should convert (T | undefined)[] type to JSONParsedT | null>[]', () => {100 type Actual = JSONParsed<(number | undefined)[]>101 type Expected = (number | null)[]102 expectTypeOf<Actual>().toEqualTypeOf<Expected>()103 })

fix(types): handle readonly array correctly (#3172)m-shakabefore adoptionbb14b1e

lines 104–108why, one step back
104 it('should convert { key: readonly T[]} correctly', () => {
105 type Actual = JSONParsed<{ key: readonly number[] }>106 type Expected = { key: readonly number[] }107 expectTypeOf<Actual>().toEqualTypeOf<Expected>()108 })

feat(types): improve JSONParsed (#3074)m-shakabefore adoption2a4f257

lines 109–169why, one step back
109 })
110111 describe('tuple', () => {112 it('should convert [T, S] type to [T, S]', () => {113 type Actual = JSONParsed<[number, string]>114 type Expected = [number, string]115 expectTypeOf<Actual>().toEqualTypeOf<Expected>()116 })117 it('should convert [T, undefined] type to [T, null]', () => {118 type Actual = JSONParsed<[number, undefined]>119 type Expected = [number, null]120 expectTypeOf<Actual>().toEqualTypeOf<Expected>()121 })122 })123124 describe('object', () => {125 it('should omit keys with undefined value', () => {126 type Actual = JSONParsed<{ a: number; b: undefined }>127 type Expected = { a: number }128 expectTypeOf<Actual>().toEqualTypeOf<Expected>()129 })130 it('should omit keys with symbol value', () => {131 type Actual = JSONParsed<{ a: number; b: symbol }>132 type Expected = { a: number }133 expectTypeOf<Actual>().toEqualTypeOf<Expected>()134 })135 it('should omit keys with function value', () => {136 type Actual = JSONParsed<{ a: number; b: () => void }>137 type Expected = { a: number }138 expectTypeOf<Actual>().toEqualTypeOf<Expected>()139 })140 it('should omit symbol keys', () => {141 type Actual = JSONParsed<{ a: number; [x: symbol]: number }>142 type Expected = { a: number }143 expectTypeOf<Actual>().toEqualTypeOf<Expected>()144 })145 it('should convert T | undefined to T | undefined', () => {146 type Actual = JSONParsed<{ a: number; b: number | undefined }>147 type Expected = { a: number; b: number | undefined }148 expectTypeOf<Actual>().toEqualTypeOf<Expected>()149 })150 it('should omit keys with invalid union', () => {151 type Actual = JSONParsed<{ a: number; b: undefined | symbol }>152 type Expected = { a: number }153 expectTypeOf<Actual>().toEqualTypeOf<Expected>()154 })155 })156157 describe('Set/Map', () => {158 it('should convert Set to empty object', () => {159 type Actual = JSONParsed<Set<number>>160 type Expected = {}161 expectTypeOf<Actual>().toEqualTypeOf<Expected>()162 })163 it('should convert Map to empty object', () => {164 type Actual = JSONParsed<Map<number, number>>165 type Expected = {}166 expectTypeOf<Actual>().toEqualTypeOf<Expected>()167 })168 })169

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 170–217why, one step back
170 it('Should parse a complex type', () => {
171 const sample: JSONParsed<SampleType> = {172 someMeta: {173 metadata: {174 href: '',175 sampleEnum: SampleEnum.Value1,176 },177 },178 }179 expectTypeOf(sample).toEqualTypeOf<SampleType>()180 })181182 it('Should parse a complex interface', () => {183 const sample: JSONParsed<SampleInterface> = {184 someMeta: {185 metadata: {186 href: '',187 sampleEnum: SampleEnum.Value1,188 },189 },190 }191 expectTypeOf(sample).toEqualTypeOf<SampleInterface>()192 })193194 it('Should convert Date to string', () => {195 type Post = {196 datetime: Date197 }198 type Expected = {199 datetime: string200 }201 type Actual = JSONParsed<Post>202 // eslint-disable-next-line @typescript-eslint/no-unused-vars203 type verify = Expect<Equal<Expected, Actual>>204 })205206 it('Should convert bigint to never', () => {207 type Post = {208 num: bigint209 }210 type Expected = {211 num: never212 }213 type Actual = JSONParsed<Post>214 // eslint-disable-next-line @typescript-eslint/no-unused-vars215 type verify = Expect<Equal<Expected, Actual>>216 })217})