Skip to content

Commit

Permalink
fix: Correctly detect index access types in typescript (#400)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Tschinder <231804+danez@users.noreply.github.com>
  • Loading branch information
bryceosterhaus and danez authored May 28, 2022
1 parent ab8b177 commit 85ea6a5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/__snapshots__/main-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,24 @@ Object {
}
`;
exports[`main fixtures processes component "component_41.tsx" without errors 1`] = `
Object {
"description": "",
"displayName": "MyComponent",
"methods": Array [],
"props": Object {
"value": Object {
"description": "String value of a number",
"required": false,
"tsType": Object {
"name": "STRING_VALS[number]",
"raw": "typeof STRING_VALS[number]",
},
},
},
}
`;
exports[`main fixtures processes component "flow-export-type.js" without errors 1`] = `
Object {
"description": "This is a Flow class component",
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/fixtures/component_41.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';

export const STRING_VALS = [
'one',
'two',
'three'
];

interface IProps {
/**
* String value of a number
*/
value?: typeof STRING_VALS[number];
}

const MyComponent = (props: IProps) => {
return (
<div>
{props.value}
</div>
);
}

export default MyComponent;
20 changes: 20 additions & 0 deletions src/utils/__tests__/getTSType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,26 @@ describe('getTSType', () => {
});
});

it('resolves indexed access of array', () => {
const typePath = statement(`
var x: typeof STRING_VALS[number];
const STRING_VALS = [
'one',
'two',
'three'
];
`)
.get('declarations', 0)
.get('id')
.get('typeAnnotation')
.get('typeAnnotation');
expect(getTSType(typePath, null, noopImporter)).toEqual({
name: 'STRING_VALS[number]',
raw: 'typeof STRING_VALS[number]',
});
});

it('can resolve indexed access to imported type', () => {
const typePath = statement(`
var x: A["x"] = 2;
Expand Down
6 changes: 4 additions & 2 deletions src/utils/getTSType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,14 @@ function handleTSIndexedAccessType(
// We only get the signature if the objectType is a type (vs interface)
if (!objectType.signature)
return {
name: `${objectType.name}[${indexType.value.toString()}]`,
name: `${objectType.name}[${
indexType.value ? indexType.value.toString() : indexType.name
}]`,
raw: printValue(path),
};
const resolvedType = objectType.signature.properties.find(p => {
// indexType.value = "'foo'"
return p.key === indexType.value.replace(/['"]+/g, '');
return indexType.value && p.key === indexType.value.replace(/['"]+/g, '');
});
if (!resolvedType) {
return { name: 'unknown' };
Expand Down

0 comments on commit 85ea6a5

Please sign in to comment.