33import * as printers from "./index" ;
44import { opts } from "../options" ;
55import { 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
732const identifiers = Object . create ( null ) ;
833Object . 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