Skip to content

Commit 40c3587

Browse files
bryceosterhausdanez
andcommitted
fix: Correctly detect index access types in typescript (#400)
Co-authored-by: Daniel Tschinder <[email protected]> # Conflicts: # src/__tests__/__snapshots__/main-test.js.snap # src/utils/__tests__/getTSType-test.js
1 parent 9b65a55 commit 40c3587

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,3 +1577,21 @@ Object {
15771577
},
15781578
}
15791579
`;
1580+
1581+
exports[`main fixtures processes component "component_41.tsx" without errors 1`] = `
1582+
Object {
1583+
"description": "",
1584+
"displayName": "MyComponent",
1585+
"methods": Array [],
1586+
"props": Object {
1587+
"value": Object {
1588+
"description": "String value of a number",
1589+
"required": false,
1590+
"tsType": Object {
1591+
"name": "STRING_VALS[number]",
1592+
"raw": "typeof STRING_VALS[number]",
1593+
},
1594+
},
1595+
},
1596+
}
1597+
`;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
3+
export const STRING_VALS = [
4+
'one',
5+
'two',
6+
'three'
7+
];
8+
9+
interface IProps {
10+
/**
11+
* String value of a number
12+
*/
13+
value?: typeof STRING_VALS[number];
14+
}
15+
16+
const MyComponent = (props: IProps) => {
17+
return (
18+
<div>
19+
{props.value}
20+
</div>
21+
);
22+
}
23+
24+
export default MyComponent;

src/utils/__tests__/getTSType-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,26 @@ describe('getTSType', () => {
377377
});
378378
});
379379

380+
it('resolves indexed access of array', () => {
381+
const typePath = statement(`
382+
var x: typeof STRING_VALS[number];
383+
384+
const STRING_VALS = [
385+
'one',
386+
'two',
387+
'three'
388+
];
389+
`)
390+
.get('declarations', 0)
391+
.get('id')
392+
.get('typeAnnotation')
393+
.get('typeAnnotation');
394+
expect(getTSType(typePath)).toEqual({
395+
name: 'STRING_VALS[number]',
396+
raw: 'typeof STRING_VALS[number]',
397+
});
398+
});
399+
380400
it('resolves types in scope', () => {
381401
const typePath = statement(`
382402
var x: MyType = 2;

src/utils/getTSType.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,14 @@ function handleTSIndexedAccessType(
365365
// We only get the signature if the objectType is a type (vs interface)
366366
if (!objectType.signature)
367367
return {
368-
name: `${objectType.name}[${indexType.value.toString()}]`,
368+
name: `${objectType.name}[${
369+
indexType.value ? indexType.value.toString() : indexType.name
370+
}]`,
369371
raw: printValue(path),
370372
};
371373
const resolvedType = objectType.signature.properties.find(p => {
372374
// indexType.value = "'foo'"
373-
return p.key === indexType.value.replace(/['"]+/g, '');
375+
return indexType.value && p.key === indexType.value.replace(/['"]+/g, '');
374376
});
375377
if (!resolvedType) {
376378
return { name: 'unknown' };

0 commit comments

Comments
 (0)