我可能在文档中遗漏了一些内容,但是我无法在typescript中找到任何方法来获取函数中的参数类型.也就是说,我有一个功能
function test(a: string, b: number) {
console.log(a);
console.log(b)
}
Run Code Online (Sandbox Code Playgroud)
我想要访问类型,string并number可能作为元组.
我知道我可以获得函数本身的类型typeof test,或者返回类型via ReturnType<test>.
当我尝试时keyof typeof test,它又回来了never,我也无法解释.
像这样的其他答案指出extends,但我真的不明白它是如何工作的,并没有给我一个简单的方法来访问作为一种类型的所有参数集.
decltype在C++中返回一个表达式的类型,例如decltype(1+1)将是int.
请注意,表达式不会被执行或编译.
打字稿有类似的东西吗?
我认为应该工作的示例用例:
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());
Run Code Online (Sandbox Code Playgroud) typescript ×2