Skip to content

Commit

Permalink
chore: add support for non-imported TS index access (atlassian#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound authored and Noviny committed Nov 29, 2018
1 parent 08a2a31 commit ba9a419
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
52 changes: 47 additions & 5 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4093,19 +4093,61 @@ Object {
}
`;

exports[`typescript indexed type 1`] = `
exports[`typescript indexed imported type 1`] = `
Object {
"classes": Array [
Object {
"kind": "generic",
"kind": "object",
"members": Array [
Object {
"key": Object {
"kind": "id",
"name": "foo",
},
"kind": "property",
"optional": false,
"value": Object {
"kind": "generic",
"value": Object {
"kind": "id",
"name": "ImportedType['props']",
},
},
},
],
"name": Object {
"kind": "id",
"name": "Component",
"type": null,
},
"value": Object {
"kind": "object",
"name": "MyType['props']",
},
],
"kind": "program",
}
`;

exports[`typescript indexed type 1`] = `
Object {
"classes": Array [
Object {
"kind": "object",
"members": Array [
Object {
"key": Object {
"kind": "id",
"name": "foo",
},
"kind": "property",
"optional": false,
"value": Object {
"kind": "number",
},
},
],
"name": Object {
"kind": "id",
"name": "Component",
"type": null,
},
},
],
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,14 @@ converters.TSIndexedAccessType = (path, context) => {
const indexKey = path.get('indexType').node.literal.value;

if (type.kind === 'generic') {
if (type.value.members) {
const member = type.value.members.find((member) =>
member.key.name === indexKey
);
if (member) {
return member.value;
}
}
return {
kind: 'generic',
value: {
Expand Down
11 changes: 9 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,10 +1114,17 @@ const TESTS = [
typeSystem: 'typescript',
code: `
type MyType = {
props: RecursiveType
props: number;
}
class Component extends React.Component<MyType['props']> {}
class Component extends React.Component<{ foo: MyType['props'] }> {}
`
},
{
name: 'typescript indexed imported type',
typeSystem: 'typescript',
code: `
class Component extends React.Component<{ foo: ImportedType['props'] }> {}
`
},
];
Expand Down

0 comments on commit ba9a419

Please sign in to comment.