【发布时间】:2020-11-21 07:14:21
【问题描述】:
C++ 中的decltype 返回表达式的类型,例如decltype(1+1) 将是int。
请注意,表达式不会被执行或编译。
Typescript 有类似的东西吗?
我认为应该工作的示例用例:
const foo = () => ({a: '', b: 0});
type foo_return_type = decltype(foo());
// foo_return_type should be '{a: stirng, b: number}`
import { bar } from './some_module';
type my_type = decltype(new bar().some_method());
【问题讨论】:
-
在类型位置最接近的是
typeof,但它仅适用于变量,不适用于表达式。见github.com/Microsoft/TypeScript/issues/6606 -
我已经问过similar question(在找到你之前),看起来运气不好:(
-
我在谷歌上搜索“typescript 计算接口”、“typecript 推断返回类型接口”等,但没有成功,直到我记得 c++ 等价物。 [也许这条评论对 SEO 有帮助?]
标签: typescript