首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型记录:错误TS2693:'Note‘仅指类型,但在这里用作值

类型记录:错误TS2693:'Note‘仅指类型,但在这里用作值
EN

Stack Overflow用户
提问于 2021-05-27 15:15:38
回答 1查看 4.9K关注 0票数 0

我试图在我的消防基础项目中使用打字本,并得到以下错误。

代码语言:javascript
复制
 error TS2693: 'Note' only refers to a type, but is being used as a value here.

我使用了以下代码

代码语言:javascript
复制
interface Note {
 image: string;
 name: string;
 pdf: string;
}

const itemname = Note.name;

但这似乎行不通

这是进口品

代码语言:javascript
复制
import {
AngularFirestore,
AngularFirestoreCollection,
AngularFirestoreDocument,
} from '@angular/fire/firestore';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';

这是另一部分

代码语言:javascript
复制
  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();
   }
  }
EN

回答 1

Stack Overflow用户

发布于 2021-05-27 15:52:14

interface (或type)可以告诉你某事的形状。

代码语言:javascript
复制
// A `tree` has a number of `branches`.
interface Tree {
  branches: number
}

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

代码语言:javascript
复制
// This `tree` has 34 `branches`.
const myTree: Tree = { branches: 34 }

在作业期间,你不能把这两者混合在一起。以您为例,您会问:“branches有多少myTreeBranches?”它会说"typeof Tree.branches is number",这是在JavaScript中不有用或不受支持的信息。

代码语言:javascript
复制
// Doesn't work
const myTreeBranches = Tree.branches // typeof `Tree.branches` is `number`. 

你可能想要其中之一:

代码语言:javascript
复制
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`
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67725120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档