-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmigration.test-d.ts
More file actions
53 lines (46 loc) · 1.8 KB
/
Copy pathmigration.test-d.ts
File metadata and controls
53 lines (46 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { object, number, string } from '@metamask/superstruct';
import type { Json } from '@metamask/utils';
import { expectType } from 'tsd';
import { createMigrations } from './migration';
import type { MigrationChain, MigrationResult } from './migration';
// `createMigrations()` starts an empty chain typed to accept `Json`.
expectType<MigrationChain<Json>>(createMigrations());
// A step's `migrate` receives the previous step's output shape, with no cast needed.
createMigrations()
.add({ migrate: (): { count: number } => ({ count: 1 }) })
.add({
migrate: (data) => {
expectType<number>(data.count);
return data;
},
});
// A step's `migrate` must accept the previous step's output shape.
createMigrations()
.add({ migrate: (): { count: number } => ({ count: 1 }) })
// @ts-expect-error [test] `data` is `{ count: number }`, not `{ label: string }`.
.add({ migrate: (data: { label: string }) => data.label });
// `inputSchema` narrows `migrate`'s input to the schema's inferred type, with no cast
// needed.
createMigrations().add({
inputSchema: object({ oldCount: number() }),
migrate: (data) => {
expectType<number>(data.oldCount);
return { count: data.oldCount };
},
});
// `inputSchema` must be compatible with the chain's current data type, just like
// `migrate`.
createMigrations()
.add({ migrate: (): { count: number } => ({ count: 1 }) })
.add({
// @ts-expect-error [test] `inputSchema`'s inferred type doesn't extend `{ count: number }`.
inputSchema: object({ label: string() }),
migrate: () => 'x',
});
// `apply()` resolves to the final step's output type.
const migrationsForApply = createMigrations().add({
migrate: (): { count: number } => ({ count: 1 }),
});
expectType<Promise<MigrationResult<{ count: number }>>>(
migrationsForApply.apply({}),
);