我有一个界面Action
:
interface Action {}
Run Code Online (Sandbox Code Playgroud)
并实现一个Action
SpecificAction
:
class SpecificAction implements Action {
payload?: Any
}
Run Code Online (Sandbox Code Playgroud)
在TS中是否可以构造一个切换运算符,如下所示:
let action: Action
switch (action) {
case SpecificAction: //it works
console.log(action.payload) // it doesn't
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下是否有可能知道该动作已经是SpecificAction
类型?
我有一个抽象类,以及在数组中扩展它的类。我该输入什么数组?
abstract class AbstractClassToExtend {
constructor() { console.log("hello") }
}
class One extends AbstractClassToExtend {}
class Two extends AbstractClassToExtend {}
const array = [One, Two] // what do i type this array?
Run Code Online (Sandbox Code Playgroud)
我已经尝试过了const array: typeof AbstractClassToExtend[] = [One, Two]
,但是当创建数组中的类之一的实例时,
new array[0]()
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误:
error TS2511: Cannot create an instance of an abstract class.
Run Code Online (Sandbox Code Playgroud)
我正在使用 Typescript 4.3.2。
我正在学习 TypeScript。
我在学习的时候有个问题
const arr = [['192.168.0.1', 1234], ['192.168.0.2', 5678], ...];
Run Code Online (Sandbox Code Playgroud)
如何在像上面这样的二维数组中包含不同类型?
使用“any”会很好,但我在官方文档中不推荐它。