Skip to content

Commit 9eed5b7

Browse files
committed
fix: Support all literal types in typescript
1 parent 7a6ee68 commit 9eed5b7

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/utils/__tests__/getTSType-test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,16 @@ describe('getTSType', () => {
112112
});
113113
});
114114

115-
it('detects literal types', () => {
116-
const literalTypes = ['"foo"', 1234, true];
115+
describe('literal types', () => {
116+
const literalTypes = ['"foo"', 1234, true, -1, '`foo`'];
117117

118118
literalTypes.forEach(value => {
119-
const typePath = typeAlias(`let x: ${value};`);
120-
expect(getTSType(typePath)).toEqual({
121-
name: 'literal',
122-
value: `${value}`,
119+
it(`detects ${value}`, () => {
120+
const typePath = typeAlias(`let x: ${value};`);
121+
expect(getTSType(typePath)).toEqual({
122+
name: 'literal',
123+
value: `${value}`,
124+
});
123125
});
124126
});
125127
});

src/utils/getTSType.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ function getTSTypeWithResolvedTypes(
474474
const literal = path.get('literal');
475475
type = {
476476
name: 'literal',
477-
// @ts-ignore TODO create test for UnaryExpression (eg. -2 as literal)
478-
value: (literal.node.extra?.raw as string) || `${literal.node.value}`,
477+
value: printValue(literal),
479478
};
480479
} else if (node.type in namedTypes) {
481480
type = namedTypes[node.type](path, typeParams);

0 commit comments

Comments
 (0)