Skip to content

Commit ba9a419

Browse files
MarshallOfSoundNoviny
authored andcommitted
chore: add support for non-imported TS index access (#32)
1 parent 08a2a31 commit ba9a419

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

__snapshots__/test.js.snap

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,19 +4093,61 @@ Object {
40934093
}
40944094
`;
40954095

4096-
exports[`typescript indexed type 1`] = `
4096+
exports[`typescript indexed imported type 1`] = `
40974097
Object {
40984098
"classes": Array [
40994099
Object {
4100-
"kind": "generic",
4100+
"kind": "object",
4101+
"members": Array [
4102+
Object {
4103+
"key": Object {
4104+
"kind": "id",
4105+
"name": "foo",
4106+
},
4107+
"kind": "property",
4108+
"optional": false,
4109+
"value": Object {
4110+
"kind": "generic",
4111+
"value": Object {
4112+
"kind": "id",
4113+
"name": "ImportedType['props']",
4114+
},
4115+
},
4116+
},
4117+
],
41014118
"name": Object {
41024119
"kind": "id",
41034120
"name": "Component",
41044121
"type": null,
41054122
},
4106-
"value": Object {
4107-
"kind": "object",
4108-
"name": "MyType['props']",
4123+
},
4124+
],
4125+
"kind": "program",
4126+
}
4127+
`;
4128+
4129+
exports[`typescript indexed type 1`] = `
4130+
Object {
4131+
"classes": Array [
4132+
Object {
4133+
"kind": "object",
4134+
"members": Array [
4135+
Object {
4136+
"key": Object {
4137+
"kind": "id",
4138+
"name": "foo",
4139+
},
4140+
"kind": "property",
4141+
"optional": false,
4142+
"value": Object {
4143+
"kind": "number",
4144+
},
4145+
},
4146+
],
4147+
"name": Object {
4148+
"kind": "id",
4149+
"name": "Component",
4150+
"type": null,
41094151
},
41104152
},
41114153
],

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,14 @@ converters.TSIndexedAccessType = (path, context) => {
831831
const indexKey = path.get('indexType').node.literal.value;
832832

833833
if (type.kind === 'generic') {
834+
if (type.value.members) {
835+
const member = type.value.members.find((member) =>
836+
member.key.name === indexKey
837+
);
838+
if (member) {
839+
return member.value;
840+
}
841+
}
834842
return {
835843
kind: 'generic',
836844
value: {

test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,17 @@ const TESTS = [
11141114
typeSystem: 'typescript',
11151115
code: `
11161116
type MyType = {
1117-
props: RecursiveType
1117+
props: number;
11181118
}
11191119
1120-
class Component extends React.Component<MyType['props']> {}
1120+
class Component extends React.Component<{ foo: MyType['props'] }> {}
1121+
`
1122+
},
1123+
{
1124+
name: 'typescript indexed imported type',
1125+
typeSystem: 'typescript',
1126+
code: `
1127+
class Component extends React.Component<{ foo: ImportedType['props'] }> {}
11211128
`
11221129
},
11231130
];

0 commit comments

Comments
 (0)