Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 接口x任意_Typescript_Angularfire2_Google Cloud Firestore - Fatal编程技术网

Typescript 接口x任意

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

在这种情况下,使用“any”代替接口有什么缺点吗

首先,为项目提供一个接口:

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

  • 当您真正处理不可预测类型的对象时。即使这样,您也可能希望首先声明一个
    接口
    类型来声明一个契约(在运行时可能会履行,也可能不会履行)

  • 当您将“”用于没有相应的
    @types/*
    npm模块的JavaScript库时。这真是万不得已的办法。好消息是,环境声明现在越来越不常见了


    • 在这种情况下使用
      任何
      都是不好的做法。基本上,您放弃了TypeScript()最重要的部分之一及其好处(编译时类型检查)。除非必要,否则不要做

      我建议仅在两种情况下使用
      any

      • 当您真正处理不可预测类型的对象时。即使这样,您也可能希望首先声明一个
        接口
        类型来声明一个契约(在运行时可能会履行,也可能不会履行)

      • 当您将“”用于没有相应的
        @types/*
        npm模块的JavaScript库时。这真是万不得已的办法。好消息是,环境声明现在越来越不常见了


      是否可以使用类而不是接口?是否可以使用类而不是接口?