bni*_*and 2 casting type-conversion typescript
我的项目中有很多函数,它们以数字作为参数; 这个数字是数组索引的一半,另一半时间,它是一个光标位置(数组中两个条目之间的一个点).即使使用命名约定,这也会造成混淆.
我想强制执行以下功能正在采用预期的名义类型.
class Index extends Number {}
class CursorPosition extends Number {}
function getElement(i: Index) {}
function getRange(p1: CursorPosition, p2: CursorPosition) {}
const myIndex: Index = 6;
const myPosition: CursorPosition = 6;
getElement(1); // would like this to fail at compile time
getRange(2, 3); // would like this to fail at compile time
getElement(myPosition); // would like this to fail at compile time
getRange(myIndex, myIndex); // would like this to fail at compile time
getElement(myIndex); // would like this to pass at compile time
getRange(myPosition, myPosition); // would like this to pass at compile time
Run Code Online (Sandbox Code Playgroud)
我知道打字稿使用结构类型,这就是为什么这不会"开箱即用".
此外,我已经考虑了拳击我的变量和添加一个arbitray propery:
class myNum extends Number {
l: "1";
}
Run Code Online (Sandbox Code Playgroud)
或使用演员.
class myNum {
arb: "arbitrary property value";
}
const mn2: myNum = <any>8;
function getElement2(a: any[], i: myNum) {
return a[<any>i];
}
getElement2([], mn2);
getElement2([], 6);
Run Code Online (Sandbox Code Playgroud)
有更好的想法吗?
Tit*_*mir 11
您可以使用品牌类型:
type Index = Number & { __type: 'Index'}
type CursorPosition = Number & { __type: 'CursorPosition'}
function getElement(i: Index) {}
function getRange(p1: CursorPosition, p2: CursorPosition) {}
function indexFromNumber(n: number) :Index {
return n as any;
}
function cursorPositionFromNumber(n: number): CursorPosition {
return n as any;
}
const myIndex: Index = indexFromNumber(6);
const myPosition: CursorPosition = cursorPositionFromNumber(6);
getElement(1); // error
getRange(2, 3); // error
getElement(myPosition); // error
getRange(myIndex, myIndex); // error
getElement(myIndex); // ok
getRange(myPosition, myPosition); //ok
Run Code Online (Sandbox Code Playgroud)
您需要定义一个辅助函数来创建该类型的实例或使用类型assertions(const myIndex = 1 as any as Index),但如果您传递一个简单的数字,调用站点将给出错误.
该文章有更多关于该主题的讨论了一下.typescript编译器也将这种方法用于路径
| 归档时间: |
|
| 查看次数: |
639 次 |
| 最近记录: |