Skip to content

Commit dfc7a95

Browse files
committed
use name if value not present
1 parent 6a98907 commit dfc7a95

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/__tests__/__snapshots__/main-test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ Object {
18111811
"description": "String value of a number",
18121812
"required": false,
18131813
"tsType": Object {
1814-
"name": "STRING_VALS[undefined]",
1814+
"name": "STRING_VALS[number]",
18151815
"raw": "typeof STRING_VALS[number]",
18161816
},
18171817
},

src/utils/__tests__/getTSType-test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,26 @@ describe('getTSType', () => {
753753
});
754754
});
755755

756+
it('resolves indexed access of array', () => {
757+
const typePath = statement(`
758+
var x: typeof STRING_VALS[number];
759+
760+
const STRING_VALS = [
761+
'one',
762+
'two',
763+
'three'
764+
];
765+
`)
766+
.get('declarations', 0)
767+
.get('id')
768+
.get('typeAnnotation')
769+
.get('typeAnnotation');
770+
expect(getTSType(typePath, null, noopImporter)).toEqual({
771+
name: 'STRING_VALS[number]',
772+
raw: 'typeof STRING_VALS[number]',
773+
});
774+
});
775+
756776
it('can resolve indexed access to imported type', () => {
757777
const typePath = statement(`
758778
var x: A["x"] = 2;

src/utils/getTSType.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ function handleTSIndexedAccessType(
391391
// We only get the signature if the objectType is a type (vs interface)
392392
if (!objectType.signature)
393393
return {
394-
name: `${objectType.name}[${indexType.value &&
395-
indexType.value.toString()}]`,
394+
name: `${objectType.name}[${indexType.value ? indexType.value.toString(): indexType.name }]`,
396395
raw: printValue(path),
397396
};
398397
const resolvedType = objectType.signature.properties.find(p => {

0 commit comments

Comments
 (0)