Typescript 从模块生成类型';s自己导出的密钥
给定这样一个模块:Typescript 从模块生成类型';s自己导出的密钥,typescript,typescript2.1,Typescript,Typescript2.1,给定这样一个模块: export const a: string; export const b: string; import * as stuff from "./stuff"; type StuffKeys = keyof typeof stuff; // "a" | "b" 从外部可以生成一个类型“a”|“b”,如下所示: export const a: string; export const b: string; import * as stuff from "./stuff"
export const a: string;
export const b: string;
import * as stuff from "./stuff";
type StuffKeys = keyof typeof stuff; // "a" | "b"
从外部可以生成一个类型“a”|“b”
,如下所示:
export const a: string;
export const b: string;
import * as stuff from "./stuff";
type StuffKeys = keyof typeof stuff; // "a" | "b"
但我想在模块内生成并导出此类型。比如:
export type MyKeys = keyof typeof this;
但这是行不通的
有办法做到这一点吗 我不相信您计划做的是可能的,因为行
导出类型MyKeys…
需要包含在keys类型本身中
然而,令人惊讶的是,它只是将模块导入自身并从中导出密钥
main.ts
测试.ts
import {MyKeys} from './main';
const a : MyKeys = 'a';
const b : MyKeys = 'c'; // TS2322: Type '"c"' is not assignable to type '"a" | "b"'.
我不相信您计划做的是可能的,因为行
导出类型MyKeys…
需要包含在keys类型本身中
然而,令人惊讶的是,它只是将模块导入自身并从中导出密钥
main.ts
测试.ts
import {MyKeys} from './main';
const a : MyKeys = 'a';
const b : MyKeys = 'c'; // TS2322: Type '"c"' is not assignable to type '"a" | "b"'.