typescript - 从对象中获取项目没有错误
问题描述
有这个代码:
enum Stages {
BOOT = 'boot',
LOAD = 'load'
}
interface A { … }
interface C<T> { … }
interface List {
readonly [Stages.BOOT]: C<A>;
readonly [Stages.LOAD]: C<A>;
}
class Main {
constructor(
readonly list: List
){}
getItem <T extends A>(key: Stages): C<T> | null {
if (key in this.list) return this.list[key] as C<T>; //here is the error
//^^^^^^^^^^^^^
return null;
}
}
编译器说:
Element implicitly has an 'any' type because expression of type
'Stages' can't be used to index type 'List'.
Property '[Stages.LOAD]' does not exist on type 'List'
但是该项目存在,记录该对象显示正确的行为。这里有什么问题?
解决方案
推荐阅读
- java - [TreeMap]:在一个类中创建和加载 TreeMap,并从几个单独/不同的类中访问该 Tmap
- javascript - 如何在不占用空间的情况下隐藏 html 元素 x 时间?
- python-3.x - Mac终端flake8 --version在pip3下载后找不到flake8
- c# - List<>.Append 不适用于公共、静态和初始化字段
- r - R - ggplot2:基于数据类别的曲线下的阴影区域
- algorithm - 如何证明一般多项式情况下的 Big-omega?
- c++ - 相同的未更改代码是否有可能有时运行而有时不运行?
- node.js - 无法使用 ssl 和 apache 连接到 websocket
- java - 我怎么能用java目录和子目录中的所有文件创建一个Jtable
- mule-studio - 为什么在任何点平台中禁用“从 REST API 生成流”选项?