diff --git a/lib/__snapshots__/bi-directional.test.ts.snap b/lib/__snapshots__/bi-directional.test.ts.snap new file mode 100644 index 0000000..d5da447 --- /dev/null +++ b/lib/__snapshots__/bi-directional.test.ts.snap @@ -0,0 +1,6 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`bi-directional conversion should forward -1 back and forth 1`] = ` +"export type Num = 1000 | -1; +" +`; diff --git a/lib/bi-directional.test.ts b/lib/bi-directional.test.ts index 5754846..aa059b4 100644 --- a/lib/bi-directional.test.ts +++ b/lib/bi-directional.test.ts @@ -39,4 +39,18 @@ describe( "bi-directional conversion", ( ) => expect( epi ).toEqual( ts ); } ); + + it( "should forward -1 back and forth", ( ) => + { + const ct = convertTypeScriptToCoreTypes( + `export type Num = 1000 | -1;` + ); + + const ts = convertCoreTypesToTypeScript( + ct.data, + { noDescriptiveHeader: true, noDisableLintHeader: true } + ); + + expect( ts.data ).toMatchSnapshot( ); + } ); } ); diff --git a/lib/ts-to-core-types.test.ts b/lib/ts-to-core-types.test.ts index 316ad2a..188d355 100644 --- a/lib/ts-to-core-types.test.ts +++ b/lib/ts-to-core-types.test.ts @@ -45,6 +45,22 @@ it( "object literal type", ( ) => ] ); } ); +it( "negative numeric literal type", ( ) => +{ + const coreTypes = convertTypeScriptToCoreTypes( ` + export type Foo = -1; + ` ).data.types; + + equal( coreTypes, [ + { + name: 'Foo', + title: 'Foo', + type: 'number', + const: -1, + } + ] ); +} ); + it( "basic interface with additional properties", ( ) => { const coreTypes = convertTypeScriptToCoreTypes( ` diff --git a/lib/ts-to-core-types.ts b/lib/ts-to-core-types.ts index 4a18738..ce839f6 100644 --- a/lib/ts-to-core-types.ts +++ b/lib/ts-to-core-types.ts @@ -451,11 +451,23 @@ function fromTsTypeNode( node: ts.TypeNode, ctx: Context ) else if ( node.literal.kind === ts.SyntaxKind.NullKeyword ) return { type: 'null', ...decorateNode( node ) }; - else if ( node.literal.kind === ts.SyntaxKind.PrefixUnaryExpression ) + else if ( node.literal.kind === ts.SyntaxKind.PrefixUnaryExpression ) { + if ( + "operand" in node.literal && + node.literal.operator === ts.SyntaxKind.MinusToken + ) { + return { + type: 'number', + const: -Number( node.literal.operand.getText( ) ), + ...decorateNode( node ), + } + } + return ctx.handleError( ctx.getUnsupportedError( - "Prefix unary expressions not supported", + "Prefix unary expressions is only supported for MinusToken", node.literal ) ); + } return ctx.handleError( ctx.getUnsupportedError( "Literal type not understood",