Skip to content

Commit e4e2780

Browse files
committed
feat: partial Omit support
1 parent bf2ca3e commit e4e2780

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/__tests__/__snapshots__/utility-types.spec.ts.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`should handle Omit type 1`] = `
4+
"declare type A = $Diff<
5+
{
6+
a: string,
7+
b: number,
8+
...
9+
},
10+
{ a: any }
11+
>;
12+
declare type B = $Diff<
13+
{
14+
a: string,
15+
b: number,
16+
...
17+
},
18+
{ a: any, b: any }
19+
>;
20+
declare type O = {
21+
a: string,
22+
b: number,
23+
...
24+
};
25+
declare type U = \\"a\\";
26+
declare type C = $Diff<O, { [key: U]: any }>;
27+
"
28+
`;
29+
330
exports[`should handle utility types 1`] = `
431
"declare type A = $ReadOnly<{
532
a: number,

src/__tests__/utility-types.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,17 @@ type F2<T, U> = Record<T, U>
3030
expect(beautify(result)).toMatchSnapshot();
3131
expect(result).toBeValidFlowTypeDeclarations();
3232
});
33+
34+
it("should handle Omit type", () => {
35+
const ts = `
36+
type A = Omit<{ a: string, b: number }, "a">
37+
type B = Omit<{ a: string, b: number }, "a" | "b">
38+
39+
type O = { a: string, b: number };
40+
type U = "a";
41+
type C = Omit<O, U>;
42+
`;
43+
const result = compiler.compileDefinitionString(ts, { quiet: true });
44+
expect(beautify(result)).toMatchSnapshot();
45+
expect(result).toBeValidFlowTypeDeclarations();
46+
});

src/printers/identifiers.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
import * as printers from "./index";
44
import { opts } from "../options";
55
import { withEnv } from "../env";
6+
import ts from "typescript";
7+
8+
const Record = ([key, value]: [any, any], isInexact = opts().inexact) => {
9+
const valueType = printers.node.printType(value);
10+
11+
switch (key.kind) {
12+
case ts.SyntaxKind.LiteralType:
13+
return `{ ${printers.node.printType(key)}: ${valueType}${
14+
isInexact ? ", ..." : ""
15+
}}`;
16+
case ts.SyntaxKind.UnionType:
17+
if (key.types.every(t => t.kind === ts.SyntaxKind.LiteralType)) {
18+
const fields = key.types.reduce((acc, t) => {
19+
acc += `${printers.node.printType(t)}: ${valueType},\n`;
20+
return acc;
21+
}, "");
22+
return `{ ${fields}${isInexact ? "..." : ""}}`;
23+
}
24+
// Fallthrough
25+
default:
26+
return `{[key: ${printers.node.printType(key)}]: ${valueType}${
27+
isInexact ? ", ..." : ""
28+
}}`;
29+
}
30+
};
631

732
const identifiers = Object.create(null);
833
Object.assign(identifiers, {
@@ -22,11 +47,12 @@ Object.assign(identifiers, {
2247
typeArguments[0],
2348
)}>`;
2449
},
25-
Record: ([key, value]: [any, any]) => {
26-
const isInexact = opts().inexact;
27-
return `{[key: ${printers.node.printType(key)}]: ${printers.node.printType(
28-
value,
29-
)}${isInexact ? ", ..." : ""}}`;
50+
Record,
51+
Omit: ([obj, keys]: [any, any]) => {
52+
return `$Diff<${printers.node.printType(obj)},${Record(
53+
[keys, { kind: ts.SyntaxKind.AnyKeyword }],
54+
false,
55+
)}>`;
3056
},
3157
});
3258

0 commit comments

Comments
 (0)