Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 6876062

Browse files
authored
feat: implement shape propType (#219)
1 parent d6d7d1e commit 6876062

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Diff for: src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export function get(astq: astqts.ASTQ, propertyAst: any, propTypesName: string|u
5959
// FIXME: This should better be a real enum
6060
const enumEntries = getEnumValues(typeAst.arguments[0].elements);
6161
return getTypeDeclaration(dom.create.union(enumEntries as dom.Type[]), !required);
62+
case 'shape':
63+
const entries = typeAst.arguments[0].properties.map((entry: any) => {
64+
const typeDecl = get(astq, entry.value, propTypesName);
65+
return dom.create.property(entry.key.name, typeDecl.type,
66+
typeDecl.optional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None);
67+
});
68+
return getTypeDeclaration(dom.create.objectType(entries), !required);
6269
}
6370

6471
return {

Diff for: tests/es6-class.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ declare module 'component' {
1818
optionalEnum?: 'News' | 'Photos' | 1 | 2;
1919
optionalUnion?: string | number;
2020
optionalArrayOf?: number[];
21+
optionalObjectWithShape?: {
22+
color?: string;
23+
fontSize?: number;
24+
};
2125
requiredFunc: (...args: any[])=>any;
2226
requiredAny: any;
2327
requiredUnion: any[] | boolean;

Diff for: tests/es6-class.jsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ Component.propTypes = {
3030
]),
3131
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
3232
//optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
33-
//optionalObjectWithShape: React.PropTypes.shape({
34-
// color: React.PropTypes.string,
35-
// fontSize: React.PropTypes.number
36-
//}),
33+
optionalObjectWithShape: React.PropTypes.shape({
34+
color: React.PropTypes.string,
35+
fontSize: React.PropTypes.number
36+
}),
3737
requiredFunc: React.PropTypes.func.isRequired,
3838
requiredAny: React.PropTypes.any.isRequired,
3939
requiredUnion: React.PropTypes.oneOfType([

0 commit comments

Comments
 (0)