我需要一个有关如何在打字稿中导出函数和相关类型(接口)的基本示例。
例如,我具有以下返回IConfig的readConfig函数,并且我想知道如何将该函数和接口放入单独的config.ts文件中,然后将其导入。
interface IConfig {
db: string,
table: string,
connstring: string,
sources: Array<{
db: string,
connstring: string
}>
}
import { safeLoad } from 'js-yaml';
import { readFileSync } from 'fs';
const config: IConfig = readConfig();
// [do stuff with config]
function readConfig(configFile: string = 'configuration.yml'): IConfig {
const config: IConfig = safeLoad(readFileSync(configFile, 'utf8'));
return config;
}
答案 0 :(得分:1)
some-file.ts
export interface IConfig {
db: string,
table: string,
connstring: string,
sources: Array<{
db: string,
connstring: string
}>
}
export function readConfig(configFile: string = 'configuration.yml'): IConfig {
const config: IConfig = safeLoad(readFileSync(configFile, 'utf8'));
return config;
}
some-other-file.ts
import { safeLoad } from 'js-yaml';
import { readFileSync } from 'fs';
import { IConfig, readConfig} from 'some-file'
const config: IConfig = readConfig();
// [do stuff with config]