繁体   English   中英

Typescript:错误 TS2693:“注意”仅指一种类型,但在此处用作值

[英]Typescript: error TS2693: 'Note' only refers to a type, but is being used as a value here

我正在尝试将 Typescript 用于我的 firebase 项目,但出现以下错误。

 error TS2693: 'Note' only refers to a type, but is being used as a value here.

我使用了以下代码

interface Note {
 image: string;
 name: string;
 pdf: string;
}

const itemname = Note.name;

但这似乎不起作用

这是进口

import {
AngularFirestore,
AngularFirestoreCollection,
AngularFirestoreDocument,
} from '@angular/fire/firestore';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';

这是另一部分

  export class Advance11Component implements OnInit {
  notesCollection: AngularFirestoreCollection<Note>;
  notes: Observable<Note[]>;

  constructor(private afs: AngularFirestore) {}
  ngOnInit(): void {
  this.notesCollection = this.afs
    .collection('class11AdvancedTheory')
    .doc(itemname)
    .collection(itemname, (ref) => {
      return ref;
    });
    this.notes = this.notesCollection.valueChanges();
   }
  }

interface (或type )可以告诉您某物的形状

// A `tree` has a number of `branches`.
interface Tree {
  branches: number
}

一个实例(例如const x =... )分配一个可以在运行时引用的特定值。

// This `tree` has 34 `branches`.
const myTree: Tree = { branches: 34 }

您不能在作业期间将这两者混合使用。 以您为例,您会问“ myTreeBranches有多少个branches ?” 它会说“ typeof Tree.branches is number ”,在 JavaScript 中没有用或不支持的信息。

// Doesn't work
const myTreeBranches = Tree.branches // typeof `Tree.branches` is `number`. 

您可能想要其中之一:

interface Tree {
    branches: number
}

type TreeBranches = Tree['branches'] // extract an interface property

const myTree: Tree = { branches: 34 } // create an instance of `Tree`

const myTreeBranches: Tree['branches'] = 34 // create variable that could be assigned to
                                            //    an instance of `Tree`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM