Skip to content
Myr

types.test.ts

This file

Path

src/utils/types.test.ts

294 lines · 8.9 KiB · TypeScript

Last changed by

fix(types): strip extra properties from array types in JSONParsed (#5103)arman-luthrabefore adoptionab590e4

History of this file

Take it

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

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–118why, one step back
1import type { JSONParsed, JSONValue } 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 }2324 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 it('should convert invalid type with { toJSON() => T to T', () => {64 type Actual = JSONParsed<bigint & { toJSON(): string }>65 type Expected = string66 expectTypeOf<Actual>().toEqualTypeOf<Expected>()67 })68 })6970 describe('invalid types', () => {71 it('should convert undefined type to never', () => {72 type Actual = JSONParsed<undefined>73 type Expected = never74 expectTypeOf<Actual>().toEqualTypeOf<Expected>()75 })76 it('should convert symbol type to never', () => {77 type Actual = JSONParsed<symbol>78 type Expected = never79 expectTypeOf<Actual>().toEqualTypeOf<Expected>()80 })81 it('should convert function type to never', () => {82 type Actual = JSONParsed<() => void>83 type Expected = never84 expectTypeOf<Actual>().toEqualTypeOf<Expected>()85 })86 it('should convert bigint type to never', () => {87 type Actual = JSONParsed<bigint>88 type Expected = never89 expectTypeOf<Actual>().toEqualTypeOf<Expected>()90 })91 })9293 describe('array', () => {94 it('should convert undefined[] type to null[]', () => {95 type Actual = JSONParsed<undefined[]>96 type Expected = null[]97 expectTypeOf<Actual>().toEqualTypeOf<Expected>()98 })99 it('should convert Function[] type to null[]', () => {100 type Actual = JSONParsed<(() => void)[]>101 type Expected = null[]102 expectTypeOf<Actual>().toEqualTypeOf<Expected>()103 })104 it('should convert symbol[] type to null[]', () => {105 type Actual = JSONParsed<symbol[]>106 type Expected = null[]107 expectTypeOf<Actual>().toEqualTypeOf<Expected>()108 })109 it('should convert (T | undefined)[] type to JSONParsedT | null>[]', () => {110 type Actual = JSONParsed<(number | undefined)[]>111 type Expected = (number | null)[]112 expectTypeOf<Actual>().toEqualTypeOf<Expected>()113 })114 it('should convert { key: readonly T[]} correctly', () => {115 type Actual = JSONParsed<{ key: readonly number[] }>116 type Expected = { key: readonly number[] }117 expectTypeOf<Actual>().toEqualTypeOf<Expected>()118 })

fix(types): strip extra properties from array types in JSONParsed (#5103)arman-luthrabefore adoptionab590e4

lines 119–128why, one step back
119 it('should convert T[] with additional properties to T[]', () => {
120 type Actual = JSONParsed<{ foo: 'bar' }[] & { meta: 'v1' }>121 type Expected = { foo: 'bar' }[]122 expectTypeOf<Actual>().toEqualTypeOf<Expected>()123 })124 it('should convert elements of T[] with additional properties', () => {125 type Actual = JSONParsed<(number | undefined)[] & { meta: 'v1' }>126 type Expected = (number | null)[]127 expectTypeOf<Actual>().toEqualTypeOf<Expected>()128 })

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 129–294why, one step back
129 })
130131 describe('tuple', () => {132 it('should convert [T, S] type to [T, S]', () => {133 type Actual = JSONParsed<[number, string]>134 type Expected = [number, string]135 expectTypeOf<Actual>().toEqualTypeOf<Expected>()136 })137 it('should convert [T, undefined] type to [T, null]', () => {138 type Actual = JSONParsed<[number, undefined]>139 type Expected = [number, null]140 expectTypeOf<Actual>().toEqualTypeOf<Expected>()141 })142 })143144 describe('object', () => {145 it('should omit keys with undefined value', () => {146 type Actual = JSONParsed<{ a: number; b: undefined }>147 type Expected = { a: number }148 expectTypeOf<Actual>().toEqualTypeOf<Expected>()149 })150 it('should omit keys with symbol value', () => {151 type Actual = JSONParsed<{ a: number; b: symbol }>152 type Expected = { a: number }153 expectTypeOf<Actual>().toEqualTypeOf<Expected>()154 })155 it('should omit keys with function value', () => {156 type Actual = JSONParsed<{ a: number; b: () => void }>157 type Expected = { a: number }158 expectTypeOf<Actual>().toEqualTypeOf<Expected>()159 })160 it('should omit symbol keys', () => {161 type Actual = JSONParsed<{ a: number; [x: symbol]: number }>162 type Expected = { a: number }163 expectTypeOf<Actual>().toEqualTypeOf<Expected>()164 })165 it('should convert T | undefined to T | undefined', () => {166 type Actual = JSONParsed<{ a: number; b: number | undefined }>167 type Expected = { a: number; b: number | undefined }168 expectTypeOf<Actual>().toEqualTypeOf<Expected>()169 })170 it('should omit keys with invalid union', () => {171 type Actual = JSONParsed<{ a: number; b: undefined | symbol }>172 type Expected = { a: number }173 expectTypeOf<Actual>().toEqualTypeOf<Expected>()174 })175 })176177 describe('unknown', () => {178 it('should convert unknown type to unknown', () => {179 type Actual = JSONParsed<unknown>180 type Expected = JSONValue181 expectTypeOf<Actual>().toEqualTypeOf<Expected>()182 })183184 it('Should convert unknown value to JSONValue', () => {185 type Actual = JSONParsed<{ value: unknown }>186 type Expected = { value: JSONValue }187 expectTypeOf<Actual>().toEqualTypeOf<Expected>()188 })189 })190191 describe('Set/Map', () => {192 it('should convert Set to empty object', () => {193 type Actual = JSONParsed<Set<number>>194 type Expected = {}195 expectTypeOf<Actual>().toEqualTypeOf<Expected>()196 })197 it('should convert Map to empty object', () => {198 type Actual = JSONParsed<Map<number, number>>199 type Expected = {}200 expectTypeOf<Actual>().toEqualTypeOf<Expected>()201 })202 })203204 it('Should parse a complex type', () => {205 const sample: JSONParsed<SampleType> = {206 someMeta: {207 metadata: {208 href: '',209 sampleEnum: SampleEnum.Value1,210 },211 },212 }213 expectTypeOf(sample).toEqualTypeOf<SampleType>()214 })215216 it('Should parse a complex interface', () => {217 const sample: JSONParsed<SampleInterface> = {218 someMeta: {219 metadata: {220 href: '',221 sampleEnum: SampleEnum.Value1,222 },223 },224 }225 expectTypeOf(sample).toEqualTypeOf<SampleInterface>()226 })227228 it('Should convert Date to string', () => {229 type Post = {230 datetime: Date231 }232 type Expected = {233 datetime: string234 }235 type Actual = JSONParsed<Post>236 expectTypeOf<Actual>().toEqualTypeOf<Expected>()237 })238239 it('Should convert bigint to never', () => {240 type Post = {241 num: bigint242 }243 type Expected = never244 type Actual = JSONParsed<Post>245 expectTypeOf<Actual>().toEqualTypeOf<Expected>()246 })247248 it('Should convert bigint when TError is provided', () => {249 type Post = {250 num: bigint251 }252 type Expected = {253 num: JSONValue254 }255 type Actual = JSONParsed<Post, never>256 expectTypeOf<Actual>().toEqualTypeOf<Expected>()257 })258259 it('Should convert bigint[] to never', () => {260 type Post = {261 nums: bigint[]262 }263 type Expected = never264 type Actual = JSONParsed<Post>265 expectTypeOf<Actual>().toEqualTypeOf<Expected>()266 })267268 it('Should convert bigint[] when TError is provided', () => {269 type Post = {270 num: bigint[]271 }272 type Expected = {273 num: JSONValue[]274 }275 type Actual = JSONParsed<Post, never>276 expectTypeOf<Actual>().toEqualTypeOf<Expected>()277 })278279 it('Should parse bigint with a toJSON function', () => {280 class SafeBigInt {281 unsafe = BigInt('42')282283 toJSON() {284 return {285 unsafe: '42n',286 }287 }288 }289290 type Actual = JSONParsed<SafeBigInt>291 type Expected = { unsafe: string }292 expectTypeOf<Actual>().toEqualTypeOf<Expected>()293 })294})