简体   繁体   English

内置类型的打字稿别名

[英]Typescript alias for builtin type

How can i make alias for built in type such as fromutility-types , i have definition which have the same name as builtin types, and happen to use type with the same name from builtin types, example :我如何为内置类型创建别名,例如来自实用程序类型,我有与内置类型同名的定义,并且碰巧使用来自内置类型的同名类型,例如:

interface Record { // <-- my definition

   toObject() : Record<string, object> // <-- built in definition
}

does not work不起作用

type TsRecord<K,V> = Record<K, V>

if there is an import method, i cant find the reference如果有导入方法,我找不到参考

import {Record as TsRecord} from ?

If you're using TypeScript 3.4 or above, you can use the globalThis namespace to access global types whose names have been shadowed by local declarations:如果您使用的是 TypeScript 3.4 或更高版本,则可以使用globalThis命名空间来访问名称已被局部声明遮蔽的全局类型:

interface Record { // <-- my definition
  toObject(): globalThis.Record<string, object> // <-- built in definition
}

Okay, hope that helps;好的,希望有帮助; good luck!祝你好运!

Playground link to code Playground 链接到代码

Not sure why you have to use the name Record for your own definition but you could reuse the source for the record utility type for the custom TsRecord type:不知道为什么您必须使用名称 Record 来定义您自己,但您可以为自定义 TsRecord 类型重用记录实用程序类型的源:

type TsRecord<K extends keyof any, T> = {
  [P in K]: T;
};

interface Record {
  // <-- my definition
  toObject(): TsRecord<string, object>; 
}

Or rename your own definition:或重命名您自己的定义:

interface TsRecord {
  // <-- my definition
  toObject(): Record<string, object>; 
}

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

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