C ++中decltype的打字稿等价物?

时间:2017-08-16 01:28:01

标签: typescript

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());

1 个答案:

答案 0 :(得分:1)

您可以为此使用ReturnTypeintroduced in typescript 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;
    }

*/