3.0.2 • Published 3 years ago
ts-prop v3.0.2
ts-prop
Prop type definition of Vue3. only recommended for vue typescript.
install
npm install ts-prop
yarn add ts-prop
example
<template>
<div></div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import prop from 'ts-prop';
export const drawerProps = {
visible: prop.boolean.def(false).isRequired,
title: prop.string
};
export const drawerEmits = {
open: prop.emit<(value: boolean) => void>(),
};
export default defineComponent({
name: 'Drawer',
props: drawerProps,
emits: drawerEmits,
setup(props, { emit }) {
return {}
},
});
</script>
import prop from 'ts-prop';
interface Person {
name: string;
sex?: string;
age: number;
}
const props = {
name: prop.stringNumber.def('123').isRequired,
//{type:[String,Number],required:true,default:123}
test: prop.string,
//{type:String}
titles: prop.array<Person>().valid((value) => value.length > 0),
//{type:Array as PropType<Array<Person>>,validator:(value) => value.length > 0}
content: prop.vNode,
//{type:[Object,String] as PropType<VNode|null|string>}
style: prop.css.def({ height: '20px' }),
//{type: Object as PropType<StyleValue>,default:{ height:'20px' }}
};
api
property | default type | example |
---|---|---|
string | string | prop.string |
number | number | prop.number.def(7) |
stringNumber | string Ι number | prop.stringNumber |
boolean | boolean | prop.boolean |
stringBool | string Ι boolean | prop.stringBool |
numberBool | number Ι boolean | prop.numberBool |
symbol | symbol | prop.symbol |
date | date | prop.date |
vNode | vue.VNode Ι string Ι null | prop.vNode |
css | vue.StyleValue | prop.css |
object | Record<string, unknown> | prop.object<{name?:string,age?:number}>() |
array | Record<string, unknown>[] | prop.array<{name:string}>() |
func | ()=>void | prop.func<(value?:number)=>boolean>() recommended to use emit |
literalType | string Ι boolean Ι number | Template Literal Types Api |
Template Literal Types Api
import { defineComponent, toRefs } from 'vue';
import prop from 'ts-prop';
type Button = 'ok' | 'cancel' | 0 | true;
export default defineComponent({
name: 'LiteralType',
props: {
name: prop.literalType<Button>().def('ok'),
},
setup(props) {
const { name } = toRefs(props);
return () => <button>{name.value}</button>;
},
});
Custom Type
prop.ts
import { useProp } from 'ts-prop';
export default class DefineProp {
static stringObject<T = Record<string, unknown>>() {
return useProp<string | T>([String, Object]);
}
static get stringBoolean() {
return useProp<string | boolean>([String, Boolean]);
}
}
import prop from './prop';
const props = {
name: prop.stringObject<{ name?: string; lowerCase?: boolean }>(),
type: prop.stringBoolean.isRequired,
};
3.0.2
3 years ago
3.0.1
3 years ago
3.0.0
3 years ago
2.0.2
4 years ago
2.0.1
4 years ago
2.0.0
4 years ago
1.2.10
4 years ago
1.2.8
4 years ago
1.2.9
4 years ago
1.2.7
4 years ago
1.2.6
4 years ago
1.2.5
4 years ago
1.2.4
4 years ago
1.2.3
4 years ago
1.2.0
4 years ago
1.2.2
4 years ago
1.2.1
4 years ago
1.1.0
4 years ago
1.0.10
4 years ago
1.0.9
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.6
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago