Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for TSMappedType #362

Merged
merged 1 commit into from Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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