Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #208 from MozillaReality/typescript-proptype-defaults
Browse files Browse the repository at this point in the history
Handle component property default values correctly
  • Loading branch information
fernandojsg authored Jul 3, 2020
2 parents c0ea632 + 3cec8a9 commit 5bf62df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/Component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { PropType } from "./Types";
* Base class for components.
*/

export type ComponentSchemaProp<T> = {
default?: T;
type: PropType<T>;
export type ComponentSchemaProp = {
default?: any;
type: PropType<any, any>;
};

export type ComponentSchema = {
[propName: string]: ComponentSchemaProp<any>;
[propName: string]: ComponentSchemaProp;
};

export class Component<C> {
Expand Down
35 changes: 19 additions & 16 deletions src/Types.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import { Component } from "./Component";

export type TypeCopyFunction<T> = (src: T, dest: T) => T;
export type TypeCloneFunction<T> = (value: T) => T;

export interface PropTypeDefinition<T> {
export interface PropTypeDefinition<T, D> {
name: string
default: T
default: D
copy: TypeCopyFunction<T>
clone: TypeCloneFunction<T>
}

export interface PropType<T> extends PropTypeDefinition<T> {
export interface PropType<T, D> extends PropTypeDefinition<T, D> {
isType: true
}

export interface PropTypes {
Number: PropType<number>;
Boolean: PropType<boolean>;
String: PropType<string>;
Array: PropType<Array<any>>;
Ref: PropType<any>;
JSON: PropType<any>;
}

export const Types: PropTypes;
export type NumberPropType = PropType<number, number>;
export type BooleanPropType = PropType<boolean, boolean>;
export type StringPropType = PropType<string, string>;
export type ArrayPropType<T> = PropType<Array<T>, []>;
export type RefPropType<T> = PropType<T, undefined>;
export type JSONPropType = PropType<any, null>;

export const Types: {
Number: NumberPropType,
Boolean: BooleanPropType,
String: StringPropType,
Array: ArrayPropType<any>,
Ref: RefPropType<any>,
JSON: JSONPropType
};

export function copyValue<T>(src: T, dest: T): T;
export function cloneValue<T>(value: T): T;
Expand All @@ -41,4 +44,4 @@ export function cloneClonable<T>(value: T): T;
* Use createType to create custom type definitions.
* @param typeDefinition An object with create, reset and clear functions for the custom type.
*/
export function createType<T>(typeDefinition: PropTypeDefinition<T>): PropType<T>
export function createType<T, D>(typeDefinition: PropTypeDefinition<T, D>): PropType<T, D>
33 changes: 24 additions & 9 deletions src/Types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,42 @@ export const copyValue = src => src;
export const cloneValue = src => src;

export const copyArray = (src, dest) => {
const srcArray = src;
const destArray = dest;
if (!src) {
return src;
}

if (!dest) {
return src.slice();
}

destArray.length = 0;
dest.length = 0;

for (let i = 0; i < srcArray.length; i++) {
destArray.push(srcArray[i]);
for (let i = 0; i < src.length; i++) {
dest.push(dest[i]);
}

return destArray;
return dest;
};

export const cloneArray = src => src.slice();
export const cloneArray = src => src && src.slice();

export const copyJSON = src => JSON.parse(JSON.stringify(src));

export const cloneJSON = src => JSON.parse(JSON.stringify(src));

export const copyCopyable = (src, dest) => dest.copy(src);
export const copyCopyable = (src, dest) => {
if (!src) {
return src;
}

if (!dest) {
return src.clone();
}

return dest.copy(src);
};

export const cloneClonable = src => src.clone();
export const cloneClonable = src => src && src.clone();

export function createType(typeDefinition) {
var mandatoryProperties = ["name", "default", "copy", "clone"];
Expand Down

0 comments on commit 5bf62df

Please sign in to comment.