Typescript 接口x任意
在这种情况下,使用“any”代替接口有什么缺点吗 首先,为项目提供一个接口:Typescript 接口x任意,typescript,angularfire2,google-cloud-firestore,Typescript,Angularfire2,Google Cloud Firestore,在这种情况下,使用“any”代替接口有什么缺点吗 首先,为项目提供一个接口: imports ... export interface Item { name: string; } @Component({ selector: 'app-root', template: ` my template ` }) export class AppComponent { private itemsCollection: AngularFirestoreCollection<I
imports ...
export interface Item { name: string; }
@Component({
selector: 'app-root',
template: `
my template
`
})
export class AppComponent {
private itemsCollection: AngularFirestoreCollection<Item>;
items: Observable<Item[]>;
constructor(private afs: AngularFirestore) {
this.itemsCollection = afs.collection<Item>('items');
this.items = this.itemsCollection.valueChanges();
}
}
导入。。。
导出接口项{name:string;}
@组成部分({
选择器:'应用程序根',
模板:`
我的模板
`
})
导出类AppComponent{
私人物品收集:AngularFirestoreCollection;
项目:可观察;
建造商(私人afs:AngularFirestore){
this.itemsCollection=afs.collection('items');
this.items=this.itemsCollection.valueChanges();
}
}
没有接口,带有“any”
imports...
@Component({
selector: 'app-root',
template: `
my template
`
})
export class AppComponent {
private itemsCollection: AngularFirestoreCollection<any>;
items: Observable<Any[]>;
constructor(private afs: AngularFirestore) {
this.itemsCollection = afs.collection<any>('items');
this.items = this.itemsCollection.valueChanges();
}
}
导入。。。
@组成部分({
选择器:'应用程序根',
模板:`
我的模板
`
})
导出类AppComponent{
私人物品收集:AngularFirestoreCollection;
项目:可观察;
建造商(私人afs:AngularFirestore){
this.itemsCollection=afs.collection('items');
this.items=this.itemsCollection.valueChanges();
}
}
谢谢你在这种情况下使用
任何
都是不好的做法。基本上,您放弃了TypeScript()最重要的部分之一及其好处(编译时类型检查)。除非必要,否则不要做
我建议仅在两种情况下使用any
:
- 当您真正处理不可预测类型的对象时。即使这样,您也可能希望首先声明一个
类型来声明一个契约(在运行时可能会履行,也可能不会履行)接口
- 当您将“”用于没有相应的
npm模块的JavaScript库时。这真是万不得已的办法。好消息是,环境声明现在越来越不常见了@types/*
- 在这种情况下使用
任何
都是不好的做法。基本上,您放弃了TypeScript()最重要的部分之一及其好处(编译时类型检查)。除非必要,否则不要做
我建议仅在两种情况下使用any
:
- 当您真正处理不可预测类型的对象时。即使这样,您也可能希望首先声明一个
类型来声明一个契约(在运行时可能会履行,也可能不会履行)接口
- 当您将“”用于没有相应的
npm模块的JavaScript库时。这真是万不得已的办法。好消息是,环境声明现在越来越不常见了@types/*