[英]Typescript equivalent of decltype in C++?
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());
您可以为此使用ReturnType
( 在ReturnType
2.8 中引入)
function foo() {
return {a: '', b: 0}
}
class bar {
some_method() {
return {x: '', z: 0}
}
}
type fooReturnType = ReturnType<typeof foo>
/**
type fooReturnType = {
a: string;
b: number;
}
*/
type barReturnType = ReturnType<typeof bar.prototype.some_method>
/**
type barReturnType = {
x: string;
z: number;
}
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.