types.test.ts
This file
Path
src/utils/types.test.ts
284 lines · 8.4 KiB · TypeScript
Last changed by
fix(types): fix 4.9.8 regression (#4448)Aadit Olkarbefore adoption4f5a305
History of this fileTake it
The walk stopped after 200 commits: lines older than 82bdc4d point at it.
fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
line 1why, one step back1import type { JSONParsed, JSONValue } from './types'4.9.0Yusuke Wadabefore adoption82bdc4d
lines 2–62why, one step back23describe('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 })fix(types): fix 4.9.8 regression (#4448)Aadit Olkarbefore adoption4f5a305
lines 63–67why, one step back63 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 })4.9.0Yusuke Wadabefore adoption82bdc4d
lines 68–85why, one step back68 })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 })fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
lines 86–90why, one step back86 it('should convert bigint type to never', () => {87 type Actual = JSONParsed<bigint>88 type Expected = never89 expectTypeOf<Actual>().toEqualTypeOf<Expected>()90 })4.9.0Yusuke Wadabefore adoption82bdc4d
lines 91–166why, one step back91 })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 })119 })120121 describe('tuple', () => {122 it('should convert [T, S] type to [T, S]', () => {123 type Actual = JSONParsed<[number, string]>124 type Expected = [number, string]125 expectTypeOf<Actual>().toEqualTypeOf<Expected>()126 })127 it('should convert [T, undefined] type to [T, null]', () => {128 type Actual = JSONParsed<[number, undefined]>129 type Expected = [number, null]130 expectTypeOf<Actual>().toEqualTypeOf<Expected>()131 })132 })133134 describe('object', () => {135 it('should omit keys with undefined value', () => {136 type Actual = JSONParsed<{ a: number; b: undefined }>137 type Expected = { a: number }138 expectTypeOf<Actual>().toEqualTypeOf<Expected>()139 })140 it('should omit keys with symbol value', () => {141 type Actual = JSONParsed<{ a: number; b: symbol }>142 type Expected = { a: number }143 expectTypeOf<Actual>().toEqualTypeOf<Expected>()144 })145 it('should omit keys with function value', () => {146 type Actual = JSONParsed<{ a: number; b: () => void }>147 type Expected = { a: number }148 expectTypeOf<Actual>().toEqualTypeOf<Expected>()149 })150 it('should omit symbol keys', () => {151 type Actual = JSONParsed<{ a: number; [x: symbol]: number }>152 type Expected = { a: number }153 expectTypeOf<Actual>().toEqualTypeOf<Expected>()154 })155 it('should convert T | undefined to T | undefined', () => {156 type Actual = JSONParsed<{ a: number; b: number | undefined }>157 type Expected = { a: number; b: number | undefined }158 expectTypeOf<Actual>().toEqualTypeOf<Expected>()159 })160 it('should omit keys with invalid union', () => {161 type Actual = JSONParsed<{ a: number; b: undefined | symbol }>162 type Expected = { a: number }163 expectTypeOf<Actual>().toEqualTypeOf<Expected>()164 })165 })166fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
lines 167–180why, one step back167 describe('unknown', () => {168 it('should convert unknown type to unknown', () => {169 type Actual = JSONParsed<unknown>170 type Expected = JSONValue171 expectTypeOf<Actual>().toEqualTypeOf<Expected>()172 })173174 it('Should convert unknown value to JSONValue', () => {175 type Actual = JSONParsed<{ value: unknown }>176 type Expected = { value: JSONValue }177 expectTypeOf<Actual>().toEqualTypeOf<Expected>()178 })179 })1804.9.0Yusuke Wadabefore adoption82bdc4d
lines 181–225why, one step back181 describe('Set/Map', () => {182 it('should convert Set to empty object', () => {183 type Actual = JSONParsed<Set<number>>184 type Expected = {}185 expectTypeOf<Actual>().toEqualTypeOf<Expected>()186 })187 it('should convert Map to empty object', () => {188 type Actual = JSONParsed<Map<number, number>>189 type Expected = {}190 expectTypeOf<Actual>().toEqualTypeOf<Expected>()191 })192 })193194 it('Should parse a complex type', () => {195 const sample: JSONParsed<SampleType> = {196 someMeta: {197 metadata: {198 href: '',199 sampleEnum: SampleEnum.Value1,200 },201 },202 }203 expectTypeOf(sample).toEqualTypeOf<SampleType>()204 })205206 it('Should parse a complex interface', () => {207 const sample: JSONParsed<SampleInterface> = {208 someMeta: {209 metadata: {210 href: '',211 sampleEnum: SampleEnum.Value1,212 },213 },214 }215 expectTypeOf(sample).toEqualTypeOf<SampleInterface>()216 })217218 it('Should convert Date to string', () => {219 type Post = {220 datetime: Date221 }222 type Expected = {223 datetime: string224 }225 type Actual = JSONParsed<Post>fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
line 226why, one step back226 expectTypeOf<Actual>().toEqualTypeOf<Expected>()4.9.0Yusuke Wadabefore adoption82bdc4d
lines 227–232why, one step back227 })228229 it('Should convert bigint to never', () => {230 type Post = {231 num: bigint232 }fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
lines 233–241why, one step back233 type Expected = never234 type Actual = JSONParsed<Post>235 expectTypeOf<Actual>().toEqualTypeOf<Expected>()236 })237238 it('Should convert bigint when TError is provided', () => {239 type Post = {240 num: bigint241 }4.9.0Yusuke Wadabefore adoption82bdc4d
line 242why, one step back242 type Expected = {fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
line 243why, one step back243 num: JSONValue4.9.0Yusuke Wadabefore adoption82bdc4d
line 244why, one step back244 }fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
lines 245–253why, one step back245 type Actual = JSONParsed<Post, never>246 expectTypeOf<Actual>().toEqualTypeOf<Expected>()247 })248249 it('Should convert bigint[] to never', () => {250 type Post = {251 nums: bigint[]252 }253 type Expected = never4.9.0Yusuke Wadabefore adoption82bdc4d
line 254why, one step back254 type Actual = JSONParsed<Post>fix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
lines 255–282why, one step back255 expectTypeOf<Actual>().toEqualTypeOf<Expected>()256 })257258 it('Should convert bigint[] when TError is provided', () => {259 type Post = {260 num: bigint[]261 }262 type Expected = {263 num: JSONValue[]264 }265 type Actual = JSONParsed<Post, never>266 expectTypeOf<Actual>().toEqualTypeOf<Expected>()267 })268269 it('Should parse bigint with a toJSON function', () => {270 class SafeBigInt {271 unsafe = BigInt('42')272273 toJSON() {274 return {275 unsafe: '42n',276 }277 }278 }279280 type Actual = JSONParsed<SafeBigInt>281 type Expected = { unsafe: string }282 expectTypeOf<Actual>().toEqualTypeOf<Expected>()4.9.0Yusuke Wadabefore adoption82bdc4d
lines 283–284why, one step back283 })284})