简体   繁体   English

从打字稿中的文件中获取导出的函数名称

[英]Get exported function names from file in Typescript

I need to get exported function names from a file for intelliSense purpose.我需要从文件中获取导出的函数名称以用于智能感知。

let's say we have the following file with some functions:假设我们有以下具有一些功能的文件:

//demo.ts
export const foo = () => {};
export const bor = () => {};

Now let import everything from the file and passes it to a generic function:现在让我们从文件中导入所有内容并将其传递给一个通用函数:

import * as demo from './demo'

function myGenericFunction<T>(module: T, functionName: ? ){
}

myGenericFunction(demo ,'foo');

The question is how to set the type of functionName to become like foo|bar .问题是如何将functionName的类型设置为foo|bar

You can use keyof T just as you would for the key of any other type:您可以像使用任何其他类型的键一样使用keyof T

import * as demo from './demo'

function myGenericFunction<T>(module: T, functionName: keyof T ){
}

myGenericFunction(demo ,'foo');
myGenericFunction(demo ,'bor');
myGenericFunction(demo ,'bar'); // error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM