vue组件props转为TS类型
import { ExtractPropTypes } from 'vue';
const props = {
layout: {
type: String as PropType<'inline' | 'grid' | 'vertical'>,
default: 'inline',
},
labelWidth: {
type: [Number, String] as PropType<number | 'auto'>,
default: 'auto',
},
colon: {
type: Boolean,
},
onChange: Function as PropType<(val: string) => void>,
};
// 语句
type Props = ExtractPropTypes<typeof props>;
// 最终类型的值
// type Props = {
// layout: 'inline' | 'grid' | 'vertical';
// labelWidth: number | 'auto';
// colon: boolean;
// onChange?: ((val: string) => void) | undefined
// };
取一个函数返回值的类型
function getVal() {
return {
name: 'jack',
list: [
{
type: 'string',
key: 'a',
},
],
status: true,
};
}
// 语句
type Val = ReturnType<typeof getVal>
// 最终类型的值
// type Val = {
// name: string;
// list: { type: string; key: string }[];
// status: boolean;
// };
将TS类型的所有值转为可选值
type Val = {
name: string;
list: { type: string; key: string }[];
status: boolean;
};
// 语句
type NewVal = Partial<Val>
// 最终类型的值
// type NewVal = {
// name?: string | undefined;
// list?: { type: string; key: string }[] | undefined;
// status?: boolean | undefined;
// };
将TS类型的所有值转为必选值
type Val = {
name?: string | undefined;
list?: { type: string; key: string }[] | undefined;
status?: boolean | undefined;
};
// 语句
type NewVal = Required<Val>
// 最终类型的值
// {
// name: string;
// list: { type: string; key: string }[];
// status: boolean;
// };
获取Promise返回值的类型
const fn= new Promise<string>((resolve) => {
resolve('')
})
type PickPromise<P> = P extends Promise<infer R> ? R : P
type PromiseType = PickPromise<typeof fn>
// 最终类型的值
// type PromiseType = string
最后编辑于 :2023.05.19 15:18:22
©著作权归作者所有,转载或内容合作请联系作者 【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。 平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。