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

Commit

Permalink
feat: implement shape propType (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnisterPeter authored Nov 4, 2016
1 parent d6d7d1e commit 6876062
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export function get(astq: astqts.ASTQ, propertyAst: any, propTypesName: string|u
// FIXME: This should better be a real enum
const enumEntries = getEnumValues(typeAst.arguments[0].elements);
return getTypeDeclaration(dom.create.union(enumEntries as dom.Type[]), !required);
case 'shape':
const entries = typeAst.arguments[0].properties.map((entry: any) => {
const typeDecl = get(astq, entry.value, propTypesName);
return dom.create.property(entry.key.name, typeDecl.type,
typeDecl.optional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None);
});
return getTypeDeclaration(dom.create.objectType(entries), !required);
}

return {
Expand Down
4 changes: 4 additions & 0 deletions tests/es6-class.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ declare module 'component' {
optionalEnum?: 'News' | 'Photos' | 1 | 2;
optionalUnion?: string | number;
optionalArrayOf?: number[];
optionalObjectWithShape?: {
color?: string;
fontSize?: number;
};
requiredFunc: (...args: any[])=>any;
requiredAny: any;
requiredUnion: any[] | boolean;
Expand Down
8 changes: 4 additions & 4 deletions tests/es6-class.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Component.propTypes = {
]),
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
//optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
//optionalObjectWithShape: React.PropTypes.shape({
// color: React.PropTypes.string,
// fontSize: React.PropTypes.number
//}),
optionalObjectWithShape: React.PropTypes.shape({
color: React.PropTypes.string,
fontSize: React.PropTypes.number
}),
requiredFunc: React.PropTypes.func.isRequired,
requiredAny: React.PropTypes.any.isRequired,
requiredUnion: React.PropTypes.oneOfType([
Expand Down

0 comments on commit 6876062

Please sign in to comment.