Skip to content

Commit 85ea6a5

Browse files
fix: Correctly detect index access types in typescript (#400)
Co-authored-by: Daniel Tschinder <[email protected]>
1 parent ab8b177 commit 85ea6a5

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,24 @@ Object {
18011801
}
18021802
`;
18031803
1804+
exports[`main fixtures processes component "component_41.tsx" without errors 1`] = `
1805+
Object {
1806+
"description": "",
1807+
"displayName": "MyComponent",
1808+
"methods": Array [],
1809+
"props": Object {
1810+
"value": Object {
1811+
"description": "String value of a number",
1812+
"required": false,
1813+
"tsType": Object {
1814+
"name": "STRING_VALS[number]",
1815+
"raw": "typeof STRING_VALS[number]",
1816+
},
1817+
},
1818+
},
1819+
}
1820+
`;
1821+
18041822
exports[`main fixtures processes component "flow-export-type.js" without errors 1`] = `
18051823
Object {
18061824
"description": "This is a Flow class component",
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.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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,14 @@ 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.toString()}]`,
394+
name: `${objectType.name}[${
395+
indexType.value ? indexType.value.toString() : indexType.name
396+
}]`,
395397
raw: printValue(path),
396398
};
397399
const resolvedType = objectType.signature.properties.find(p => {
398400
// indexType.value = "'foo'"
399-
return p.key === indexType.value.replace(/['"]+/g, '');
401+
return indexType.value && p.key === indexType.value.replace(/['"]+/g, '');
400402
});
401403
if (!resolvedType) {
402404
return { name: 'unknown' };

0 commit comments

Comments
 (0)