From ba9a4193e2530322c528ea8a19b2a10b98f7ac9e Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Thu, 29 Nov 2018 13:51:31 +1100 Subject: [PATCH] chore: add support for non-imported TS index access (#32) --- __snapshots__/test.js.snap | 52 ++++++++++++++++++++++++++++++++++---- index.js | 8 ++++++ test.js | 11 ++++++-- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/__snapshots__/test.js.snap b/__snapshots__/test.js.snap index c5d92528..1364663c 100644 --- a/__snapshots__/test.js.snap +++ b/__snapshots__/test.js.snap @@ -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, }, }, ], diff --git a/index.js b/index.js index b48c13db..90e7716e 100644 --- a/index.js +++ b/index.js @@ -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: { diff --git a/test.js b/test.js index c8935fd3..1c28a9d5 100644 --- a/test.js +++ b/test.js @@ -1114,10 +1114,17 @@ const TESTS = [ typeSystem: 'typescript', code: ` type MyType = { - props: RecursiveType + props: number; } - class Component extends React.Component {} + class Component extends React.Component<{ foo: MyType['props'] }> {} + ` + }, + { + name: 'typescript indexed imported type', + typeSystem: 'typescript', + code: ` + class Component extends React.Component<{ foo: ImportedType['props'] }> {} ` }, ];