且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法在 TypeScript 中创建扩展原始类型的名义类型?

更新时间:2022-11-21 21:24:52

没有办法做到这一点.

在 GitHub 网站上跟踪此问题的建议是度量单位.

A suggestion tracking this on the GitHub site is Units of Measure.

在未来的版本中,您将能够使用 type 来定义原语的替代名称,但这些不会有任何与之关联的检查:

In a future release, you'll be able to use type to define alternate names for the primitives, but these will not have any checking associated with them:

type lat = number;
type lon = number;
var x: lat = 43;
var y: lon = 48;
y = 'hello'; // error
x = y; // No error