TypeScript—類型定義文件(*.d.ts)

1、ts文件中引入jquery。
1.你們是否有再vue 上使用過 ts,並再 .ts文件中引用過 jquery

1.1是否是遇到過以下問題:javascript

import $ from 'jquery';
/***
Could not find a declaration file for module 'jquery'. 'd:/node-common/node_modules/jquery/dist/jquery.js' implicitly has an 'any' type.
  Try `npm install @types/jquery` if it exists or add a new declaration (.d.ts) file containing `declare module 'jquery';`
/
2.上述提示:

2.1.找不到模塊「jquery」的聲明文件vue

2.2.嘗試npm install @types/jquery (若是存在的話)java

2.3.添加一個包含declare module 'jquery'; 的聲明(.d.ts)文件node

3.嘗試安裝 npm install @types/jquery 文件

執行後,發現npm倉庫下是有這個包的,並且不須要引用該包,且項目可以正常運行了,是否是很神奇。jquery

其實這裏能夠理解爲,ts文件並不知道jquery文件到底暴露出的什麼東西,沒法獲取。.d.ts 會把jquery 文件裏面的方法屬性從新定義爲它可以認識的類型。ajax

好比說經常使用到的$.ajax;$.get,在node_modules/types/jquery/JqueryStatic.d.ts 中咱們能夠看到對其的定義typescript

interface JQueryStatic {
    ajax(settings?JQuery.AjaxSettings):Jquery.jqXHR;
    get(url: string,
        data: JQuery.PlainObject | string,
        success: JQuery.jqXHR.DoneCallback | null,
        dataType?: string): JQuery.jqXHR;
}

說到這裏你可能還不太懂 .d.ts文件怎麼去用。npm

那麼接下來和你們一塊兒分享如何在本身的項目中使用.d.ts文件,(如2.3中所說,也能夠本身建立.d.ts文件,對jquery 進行定義)。cookie

2、瞭解下TS數據類型
簡單的數據類型:

number:包括數字,浮點數,以及NAN、+-Infinity函數

string:字符串類型

boolean:布爾類型

null,undefined類型

void類型:能夠用於表示沒有返回值的函數

對象類型
type ballType = {
    color:string;
    r:number;
}
let ball : ballType = {color:'red',r:10}

interface ball2Type{
    color:string;
    r:number;
}

let ball2:ballType = {color:'red',r:0.5}

/***
interface 和 type均可以去定義類型。具體也是有區別 如interface 只能定義function、object、class類型,type 不只能夠定義此類型,還能夠定義其它類型 type msg = string; 具體差別可自行查閱資料 
*/

//順帶着說一下 可選屬性

interface ball3Type {
    color:string;
    r:number;
    index?:number // ? 把該屬性設爲可選屬性,即該屬性無關緊要
}
let ball3:ball3type = {color:'red',r:10} //正確
let ball4:ball3type = {color:'red'} //錯誤,ball3Type 須要有r屬性
let ball5:ball3type = {color:'red',r:10,index:1} //正確

//Partial<T> 實現可選屬性
//先看一下Partial 類型的定義
type Partial<T> = {
    [P in keyof T]?: T[P];
};
let ball6:Partial<ball3Type> = {}//正確
let ball7:Partial<ball3Type> = {r:10} //正確
//實際上 Partial 轉化 ball3Type 後,其類型就變成了,每一個屬性都是可選屬性,以下
type Patial_ball3Type = {
    color?:string | undefined; // | 如 js 裏面的 ‘||’ 或的意思
    r?:number | undefined;
    index?number | undefined;
}

//類型的拓展屬性
let ball8:ball3Type = {color:'red',r:10,title:'ball'} //error ,title 屬性不屬於ball3Type,因此呢,怎麼處理這種方式呢,以下:

type ball4Type = {
    color:string;
    r:number;
    index?number;
    [key:string]:string|number; // 表示,可含有string類型的key,且值爲stirng或number類型。
}
let ball9:ball4Type = {color:'red',r:10,title:'ball',x:10,y:10} //正確
函數類型
//聲明函數類型
type funType = (name:string) = >void;
let fun:funType = (name)=>{ console.log(name) }
//多態
interface fun2Type = {
    set(name:string):void;
    get(x:number):number;
}
let fun2:fun2Type = {
    set(name){},
    get(x){
        return x;
    }
}

還有一些混合類型,類型的並集,聯合,結構子類型等,

//簡單列舉下類型的並集和聯合
type A = {
    x:number;
    y:number;
}
type B = {
    z:number;
    x:number;
}
let m : A&B = {x:1,y;1,z:1}
let z:A|B = {z:1,x:1}
泛型
//泛型很重要,你們能夠查閱資料細看,這裏簡單列舉一下
//函數泛型
function get1<T>(num:T[]):T{
    return num[0]
}
//接口泛型
interface gen2Type<T>{
   getColor(ball:T):string;
   saveBall(ball:T):void;
}
//類的泛型
class gen3<T>{
    info(i:T):T{
        return i;
    }
}
3、類
//定義一個類
class A{
    //靜態成員
    static classId:number;
    //成員變量,默認爲public 公有屬性
    name:string;
    age:number|void;
    s?:string;
    //構造函數
    constructor(name:string,age?:number);
    //方法
    setS(s:string){
        this.s = s;
    }
}

//經過 new 實例化一個對象
const stu = new A('zhangsan');
const stu1 = new A('lisi',18);
stu1.setS("hello");
A.classId = 123;
console.log(`A.classId=${A.classId}`)

//類的繼承
class A {
    name:string;
    constructor(name:string){
        this.name = name;
    }
}
class B extends A {
    age:number;
    constructor(name:string,age:number){
        super(name);
        this.age = age;
    }
}
console.log(new B("zhangsan",18));
// B {name:'zhangsan',age:18}

//接口
interface Ani{
    kinds:number;
    draw(name:string):void;
    getKinds():number;
}
interface Bni extends Ani{
    time:Date;
    setTime(time:Date):void;
}

//看成變量類型
const M:Bni = {
    kinds:1,
    time:new Date(),
    draw(name:string){},
    getKinds(){
        return this.kinds;
    },
    setTime(time:Date){}
}
//看成接口
class M implements Bni {
    kinds = 1;
    time = new Date();
    draw(name:string){}
    getKinds(){
        return this.kinds;
    }
    setTime(time:Date){}
}
4、動手編寫.d.ts文件,讓ts文件識別js

新建index.js文件

module.exports = {
    url:'https://www.cnblogs.com/zcookies/'
}

新建custom.d.ts

//定義模塊
declare module '*/index.js'{
    const content: {
        url:string
    };
    export = content;
}

入口文件main.ts

import config from './index.js';
console.log(config.url);
//此時發現引入index.js 不會報錯,並且會智能提示 config下面的url 屬性;

/***
1.若是沒有custom.d.ts
代碼會拋出:Could not find a declaration file for module './index.js';
且沒有智能提示;
2.若是聲明 declare module '*.js',這樣作能夠解決掉錯誤提示,可是沒有智能提示;
3.想要正確的智能提示,須要按照js文件,輸入對應的.d.ts文件便可。
*/

你們能夠利用上面的知識點,實現對更復雜的js文件進行聲明.d.ts文件;

相關文章
相關標籤/搜索