.d.ts 文件的声明文件,如为
D3 JS 库,可以创建这样的声明文件:
declare namespace D3{
export interface Selectors { ... }
}
declare var d3: D3.Base;所以上述两个文件:
shims-tsx.d.ts , 在全局变量
global 中批量命名了数个内部模块。
shims-vue.d.ts ,意思是告诉
TypeScript
*.vue 后缀的文件可以交给
vue 模块来处理。3.6 访问修饰符:
private 、
public 、
protected其实很好理解:
默认为
public当成员被标记为
private 时,它就不能在声明它的类的外部访问,比如:
class Animal {
private name: string;
constructor(theName: string) {
this.name = theName;
}
}let a = new Animal('Cat').name; //错误,‘name'是私有的
protected 和
private 类似,但是,
protected 成员在派生类中可以访问
class Animal {
protected name: string;
constructor(theName: string) {
this.name = theName;
}
}class Rhino extends Animal {
constructor() {
super('Rhino');
}
getName() {
console.log(this.name) //此处的name就是Animal类中的name
}
}
3.7 可选参数 ( ?: )和非空断言操作符(!.)
可选参数
function buildName(firstName: string, lastName?: string) {
return firstName + ' ' + lastName
}// 错误演示
buildName("firstName", "lastName", "lastName")
// 正确演示
buildName("firstName")
// 正确演示
buildName("firstName", "lastName")
非空断言操作符:
能确定变量值一定不为空时使用。
与可选参数 不同的是,非空断言操作符不会防止出现 null 或 undefined。
let s = e!.name; // 断言e是非空并访问name属性4.
Vue 组件的
Ts 写法从 vue2.5 之后,vue 对 ts 有更好的支持。根据官方文档,vue 结合 typescript ,有两种书写方式:










