Skip to content
Myr

test(ts): fix type assertions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Alec Larson · parent 3cb0fb3 · Browse files · 1 file+128 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+128 added−54 removed

  • tests:+128 added−54 removed

1 file+128 added−54 removed

modified:__tests__/produce.ts+128 added-54 removed
72 unmodified lines
73 type State = {readonly a: number}74 type Recipe = (base?: State | boolean) => State7576 let foo = produce((x: any) => {}, {} as State)77 exactType(foo, {} as Recipe)7778 type Arg0 = Parameters<typeof foo>[0]79 type L = Parameters<typeof foo>["length"]80 type Return = ReturnType<typeof foo>81 exactType({} as Arg0, {} as (State | undefined))82 exactType(({} as any) as L, 0 as (0 | 1))83 exactType({} as Return, {} as State)84})8586it("can infer state type from recipe function", () => {87 type State = {readonly a: string} | {readonly b: string}82 type Recipe = (base: State, n: number) => State8884 let foo = produce((draft: Draft<State>, number: number) => {})85 exactType(foo, {} as Recipe)86 const state: State = foo({} as State, 3)89 let foo = produce((draft: Draft<State>) => {})9091 type Arg0 = Parameters<typeof foo>[0]92 type L = Parameters<typeof foo>["length"]93 type Return = ReturnType<typeof foo>94 exactType({} as Arg0, {} as State)95 exactType(({} as any) as L, 1 as const)96 exactType({} as Return, {} as State)97})9899it("can infer state type from recipe function with arguments", () => {100 type State = {readonly a: string} | {readonly b: string}101 type Recipe = (base: State, x: number) => State102103 let foo = produce((draft: Draft<State>, x: number) => {})94 exactType(foo, {} as Recipe)104105 type Arg0 = Parameters<typeof foo>[0]106 type Arg1 = Parameters<typeof foo>[1]107 type L = Parameters<typeof foo>["length"]108 type Return = ReturnType<typeof foo>109 exactType({} as Arg0, {} as State)110 exactType({} as Arg1, {} as number)111 exactType(({} as any) as L, 2 as const)112 exactType({} as Return, {} as State)113})114115it("can infer state type from recipe function with arguments and initial state", () => {116 type State = {readonly a: string} | {readonly b: string}99 type Recipe = (base: State | undefined, x: number) => State117118 let foo = produce((draft: Draft<State>, x: number) => {}, {} as State)102 exactType(foo, {} as Recipe)119120 type Arg0 = Parameters<typeof foo>[0]121 type Arg1 = Parameters<typeof foo>[1]122 type L = Parameters<typeof foo>["length"]123 type Return = ReturnType<typeof foo>124 exactType({} as Arg0, {} as (State | undefined))125 exactType({} as Arg1, {} as number)126 exactType(({} as any) as L, 2 as const)127 exactType({} as Return, {} as State)128})129130it("cannot infer state type when the function type and default state are missing", () => {106 const res = produce((_: any) => {})107 exactType(res, {} as (base: any) => any)131 const foo = produce((_: any) => {})132109 // slightly different type inference...110 const res2 = produce(_ => {})111 exactType(res2, {} as (base: any, ...rest: any[]) => any)133 type Arg0 = Parameters<typeof foo>[0]134 type L = Parameters<typeof foo>["length"]135 type Return = ReturnType<typeof foo>136 exactType({} as Arg0, {} as any)137 exactType(({} as any) as L, 1 as const)138 exactType({} as Return, {} as any)139})140141it("can update readonly state via curried api", () => {142 const newState = produce((draft: Draft<State>) => {
45 unmodified lines
188describe("curried producer", () => {189 it("supports rest parameters", () => {190 type State = {readonly a: 1}191165 // No initial state:166 let foo = produce((s: State, a: number, b: number) => {})167 exactType(foo, {} as (base: State, x: number, y: number) => State)168 foo({} as State, 1, 2)192 {193 // No initial state:194 let foo = produce((s: State, a: number, b: number) => {})195170 // TODO: Using argument parameters171 // let woo = produce((state: Draft<State>, ...args: number[]) => {})172 // exactType(woo, {} as (base: State, ...args: number[]) => State)173 // woo({} as State, 1, 2)196 type Arg0 = Parameters<typeof foo>[0]197 type Arg1 = Parameters<typeof foo>[1]198 type L = Parameters<typeof foo>["length"]199 type Return = ReturnType<typeof foo>200 exactType({} as Arg0, {} as (State | undefined))201 exactType({} as Arg1, {} as number)202 exactType(({} as any) as L, 3 as const)203 exactType({} as Return, {} as State)204175 // With initial state:176 let bar = produce(177 (state: Draft<State>, ...args: number[]) => {},178 {} as State179 )180 exactType(bar, {} as (181 base?: undefined | Immutable<State>,182 ...args: number[]183 ) => State)184 bar({} as State, 1, 2)185 bar({} as State)186 bar()205 foo({} as State, 1, 2)206 }207188 // When args is a tuple:189 let tup = produce(190 (state: Draft<State>, ...args: [string, ...number[]]) => {},191 {} as State192 )193 exactType(tup, {} as (194 base: State | undefined,195 arg1: string,196 ...args: number[]197 ) => State)198 tup({a: 1}, "", 2)199 tup(undefined, "", 2)208 {209 // Using argument parameters210 let woo = produce((state: Draft<State>, ...args: number[]) => {})211 woo({} as State, 1, 2)212213 type Args = Parameters<typeof woo>214 type L = Parameters<typeof woo>["length"]215 type Return = ReturnType<typeof woo>216 exactType({} as Args, {} as [State, ...number[]])217 exactType(({} as any) as L, 3 as number)218 exactType({} as Return, {} as State)219 }220221 {222 // With initial state:223 let bar = produce(224 (state: Draft<State>, ...args: number[]) => {},225 {} as State226 )227 bar({} as State, 1, 2)228 bar({} as State)229 bar()230231 type Args = Parameters<typeof bar>232 type L = Parameters<typeof bar>["length"]233 type Return = ReturnType<typeof bar>234 exactType({} as Args, {} as [State?, ...number[]])235 exactType(({} as any) as L, 3 as number)236 exactType({} as Return, {} as State)237 }238239 {240 // When args is a tuple:241 let tup = produce(242 (state: Draft<State>, ...args: [string, ...number[]]) => {},243 {} as State244 )245 tup({a: 1}, "", 2)246 tup(undefined, "", 2)247248 type Args = Parameters<typeof tup>249 type L = Parameters<typeof tup>["length"]250 type Return = ReturnType<typeof tup>251 exactType(252 {} as Args,253 {} as [State | undefined, string, ...number[]]254 )255 exactType(({} as any) as L, 3 as number)256 exactType({} as Return, {} as State)257 }258 })259260 it("can be passed a readonly array", () => {261 // No initial state:204 let foo = produce((state: string[]) => {})205 exactType(foo, {} as (base: readonly string[]) => readonly string[])206 foo([] as ReadonlyArray<string>)262 {263 let foo = produce((state: string[]) => {})264 foo([] as ReadonlyArray<string>)265208 // With initial state:209 let bar = produce(() => {}, [] as ReadonlyArray<any>)210 exactType(bar, {} as (base?: readonly any[]) => readonly any[])211 bar([] as ReadonlyArray<any>)212 bar(undefined)213 bar()266 type Args = Parameters<typeof foo>267 type L = Parameters<typeof foo>["length"]268 type Return = ReturnType<typeof foo>269 exactType({} as Args, {} as [readonly string[]])270 exactType(({} as any) as L, 3 as 1)271 exactType({} as Return, {} as readonly string[])272 }273274 {275 // With initial state:276 let bar = produce(() => {}, [] as ReadonlyArray<string>)277 bar([] as ReadonlyArray<string>)278 bar(undefined)279 bar()280281 type Args = Parameters<typeof bar>282 type L = Parameters<typeof bar>["length"]283 type Return = ReturnType<typeof bar>284 exactType({} as Args, {} as [((readonly string[]) | undefined)?])285 exactType(({} as any) as L, 3 as (0 | 1))286 exactType({} as Return, {} as readonly string[])287 }288 })289})290291it("works with return type of: number", () => {
182 unmodified lines