typescript給第三方插件添加聲明type,或者額外添加屬性

添加 ts 聲明

給無聲明文件的插件添加

直接 import "web-storage-cache" 會顯示 any,需要額外加多 type

declare module "web-storage-cache" {
  export default class WebStorageCache {
    /** wsCache.set('username', 'wqteam', {exp : 100}); 時間:秒 */
    set(name: string, value: any, option?: { exp: number }) {}
    get: any;
    delete: any;
    clear: any;
  }
}

給第三方插件額外添加

// 給element添加多兩個組件聲明
import * as element from "element-ui";

declare module "element-ui" {
  export const TreeTable: any;
  export const TreeTableColumn: any;
}

給第三方 interface 添加屬性

例如:想給 antd-upload 的 props 添加多一個屬性

import { UploadProps } from "antd/lib/upload";
declare module "antd/lib/upload" {
  export interface UploadProps {
    /** customRequest: async ({ file, onSuccess, onError } 回調(diào) */
    onSuccess?: Func;
  }
}


給window添加屬性

interface Window {
  socialShare: any;
}
// 或
declare global {
  interface Window {
    [key: string]: any;
  }
}

修改第三方插件屬性

declare module 'taro-f2' {
  import * as React from 'react'
  export interface IProps {
    onCanvasInit: any
  }
  export class F2Canvas extends React.Component<IProps, any> {
    static fixF2(F2: any) {}
  }
}

import { ComponentType } from 'react';
import { VideoProps } from '@tarojs/components/types/Video';
declare module '@tarojs/components/types/Video' {
  export interface VideoProps {
    /** hbuilder頁面內(nèi)播放 */
    playsinline?: boolean;
  }
}

擴展函數(shù)

export namespace accountLogin {
  export interface Param {
    userName: string // 用戶名
    password: string // 密碼
    clientType: string // 客戶端類型
    isRemember?: boolean //  是否記住
  }
  export interface PromiseData {
    loginSession: LoginSession
  }
}
export const accountLogin = (data:accountLogin.Param) : Promise<accountLogin.LoginSession>=>post(data)

擴展類

declare namespace Player {
  type ActionName = "move_up" | "move_down" | "move_left" | "move_right" | "run_up" | "run_down" | "run_left" | "run_right" | "";
}

class Player {
  public doActions(actionName: Player.ActionName) {
  }
}

react獲取props

import * as React from 'react';
import { Button } from 'antd';
 
type ButtonProps = React.ComponentProps<typeof Button>;

自動根據(jù)string推斷類型


type ShapeType = 'Square'| 'Circle'
interface Square {
    size: number;
}
interface Circle {
    radius: number;
}
type MutableRecord<U> = {
    [SubType in keyof U]: {
        type: SubType;
        data: U[SubType]
    };
}[keyof U];
type Shape = MutableRecord<{
    'Square': Square;
    'Circle': Circle;
}>;
const shape: Shape = {
    type: 'Circle',
    data: {
        radius: 10,
    }
};

其他功能

https://mp.weixin.qq.com/s/98T_14vcbZtFM5BfTCCzWA

// 公共類型
interface User {
  name: string;
  age: number;
  password: string;
}
type Role = "admin" | "user" | "guest";
type Role1 = "ADMIN" | "USER" | null;

// 轉(zhuǎn)大小寫
type UppercaseRole = Uppercase<Role>; 
type LowercaseRole = Lowercase<Role>; 
// 首字母大寫
type CapitalizeRole = Capitalize<Role>;
type UncapitalizeRole = Uncapitalize<Role>;
// Exclude 屬性排除
type NonAdmin = Exclude<Role, "admin"|"user">; // guest
// NonNullableRole 排除null和undefined
type NonNullableRole = NonNullable<Role1>; // "ADMIN" | "USER"

// Partial 全部改可選屬性
type PartialUser = Partial<User>;
// 必填
type RequiredUser = Required<User>;
// 只讀
type ReadonlyUser = Readonly<User>;
// Pick 挑選屬性組合
type UserPartial = Pick<User, "name" | "age">;
// Omit 剔除某些屬性
type UserPartialOmit = Omit<User, "password">;
// 構(gòu)造一個對象類型Record, 比較少用
interface Address {
  street: string;
  pin: number;
}
type AddressesRecord = Record<"home" | "office", Address>;

// 獲取某函數(shù)返回值類型
function funcA(){
 return '';
}
type someType = ReturnType<typeof funcA>;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容