Skip to content

Commit

Permalink
feat(types): add Prop to main type declaration file (vuejs#6856)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdaber authored and hefeng committed Jan 25, 2019
1 parent d7f4e7c commit 9c23b7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
ComponentOptions,
FunctionalComponentOptions,
RenderContext,
PropType,
PropOptions,
ComputedOptions,
WatchHandler,
Expand Down
8 changes: 5 additions & 3 deletions types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,14 @@ export interface RenderContext<Props=DefaultProps> {
injections: any
}

export type Prop<T> = { (): T } | { new (...args: any[]): T & object }
export type Prop<T> = { (): T } | { new(...args: any[]): T & object }

export type PropValidator<T> = PropOptions<T> | Prop<T> | Prop<T>[];
export type PropType<T> = Prop<T> | Prop<T>[];

export type PropValidator<T> = PropOptions<T> | PropType<T>;

export interface PropOptions<T=any> {
type?: Prop<T> | Prop<T>[];
type?: PropType<T>;
required?: boolean;
default?: T | null | undefined | (() => T | null | undefined);
validator?(value: T): boolean;
Expand Down
30 changes: 23 additions & 7 deletions types/test/options-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue, { VNode } from "../index";
import Vue, { PropType, VNode } from "../index";
import { ComponentOptions, Component } from "../index";
import { CreateElement } from "../vue";

Expand Down Expand Up @@ -59,20 +59,36 @@ class Cat {
private u = 1
}

interface IUser {
foo: string,
bar: number
}

interface ICat {
foo: any,
bar: object
}

Vue.component('union-prop', {
props: {
primitive: [String, Number],
cat: Object as PropType<ICat>,
complexUnion: { type: [User, Number] as PropType<User | number> },
kittyUser: Object as PropType<ICat & IUser>,
mixed: [RegExp, Array],
object: [Cat, User],
primitive: [String, Number],
regex: RegExp,
mixed: [RegExp, Array],
union: [User, Number] as {new(): User | Number}[] // requires annotation
union: [User, Number] as PropType<User | number>
},
data() {
this.primitive;
this.cat;
this.complexUnion;
this.kittyUser;
this.mixed;
this.object;
this.union;
this.primitive;
this.regex.compile;
this.mixed;
this.union;
return {
fixedSize: this.union,
}
Expand Down

0 comments on commit 9c23b7c

Please sign in to comment.