Skip to content

Commit

Permalink
Add support for TSMappedType (#362)
Browse files Browse the repository at this point in the history
Refs #361
  • Loading branch information
ahutchings authored and danez committed Jul 14, 2019
1 parent 92ecb87 commit e59360e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/utils/__tests__/getTSType-test.js
Expand Up @@ -429,6 +429,46 @@ describe('getTSType', () => {
});
});

it('handles mapped types', () => {
const typePath = statement(`
var x: { [key in 'x' | 'y']: boolean};
`)
.get('declarations', 0)
.get('id')
.get('typeAnnotation')
.get('typeAnnotation');

expect(getTSType(typePath)).toEqual({
name: 'signature',
type: 'object',
raw: "{ [key in 'x' | 'y']: boolean}",
signature: {
properties: [
{
key: {
elements: [
{
name: 'literal',
value: "'x'",
},
{
name: 'literal',
value: "'y'",
},
],
name: 'union',
raw: "'x' | 'y'",
required: true,
},
value: {
name: 'boolean',
},
},
],
},
});
});

describe('React types', () => {
function test(type, expected) {
const typePath = statement(`
Expand Down
29 changes: 29 additions & 0 deletions src/utils/getTSType.js
Expand Up @@ -50,6 +50,7 @@ const namedTypes = {
TSUnionType: handleTSUnionType,
TSFunctionType: handleTSFunctionType,
TSIntersectionType: handleTSIntersectionType,
TSMappedType: handleTSMappedType,
TSTupleType: handleTSTupleType,
TSTypeQuery: handleTSTypeQuery,
TSTypeOperator: handleTSTypeOperator,
Expand Down Expand Up @@ -214,6 +215,34 @@ function handleTSIntersectionType(
};
}

function handleTSMappedType(
path: NodePath,
typeParams: ?TypeParameters,
): FlowObjectSignatureType {
const key = getTSTypeWithResolvedTypes(
path.get('typeParameter').get('constraint'),
typeParams,
);
key.required = !path.node.optional;

return {
name: 'signature',
type: 'object',
raw: printValue(path),
signature: {
properties: [
{
key,
value: getTSTypeWithResolvedTypes(
path.get('typeAnnotation'),
typeParams,
),
},
],
},
};
}

function handleTSFunctionType(
path: NodePath,
typeParams: ?TypeParameters,
Expand Down

0 comments on commit e59360e

Please sign in to comment.