javascript - Typescript 定义类类型而不定义 JS 类?
问题描述
我想为具有某些属性的类创建一个类类型。例如:
class Cat {
name = 'cat';
}
class Dog {
name = 'dog';
}
type Animal = ???;
function foo(AnimalClass: Animal) {
console.log((new AnimalClass()).name);
}
我想doSomething
接受任何具有字符串name
属性的类。我能做到这一点的唯一方法是:
class _Animal {
name: string;
}
type Animal = typeof _Animal;
有没有办法在不定义新的 JS 类的情况下做到这一点?我只想要类型。
解决方案
推荐阅读
- java - 在java中使用+声明一个长字符串
- node.js - 使用日期选择器限制天数
- sql - golang sql指针值不断重复
- ios - Redis ios客户端以安全方式使用websocket
- reactjs - 无法读取 null 的属性“ownerDocument”
- module - 如何将 lmod 与鱼一起使用?
- flowtype - 带有联合参数的函数间接导致联合成员出现莫名其妙的错误
- database - 我如何阅读 blcokfile_xxxx
- python - SQLalchemy-如何从 sqlite 文件中提取表?
- api - YouTube 频道:列表 API 未按顺序返回