问题
在Typescript的严格模式(严格的null检查)下,我在使用可迭代对象(如果我的理解是正确的话)时遇到了一些问题。我想要使用'String.prototype.match()'返回的对象。
const matchLetter: RegExpMatchArray | null = points[0].match(/[a-zA-Z]/);
const direction: Tdirection = matchLetter[0];
// Two errors:
// 1. Object is possibly 'null'. ts(2531)
// 2. Type 'string' is not assignable to type 'Tdirection'. ts(2322)
我想要做什么:
// don't change RegExpMatchArray, it's from typescript lib.es5.d.ts
interface RegExpMatchArray extends Array<string> { index?: number; input?: string; }
// custom types
type Tlocation = { x: number; y: number };
type Tdirection = "R" | "U" | "L" | "D";
// demo data
const pathToPlot = [["R0", "R1", "R2"],["U0", "U1"],["L0"],["D0"]];
// demo operations
const operations = {
R: (index: number, lastLocation: Tlocation) => { return { x: lastLocation.x + index, y: lastLocation.y }},
U: (index: number, lastLocation: Tlocation) => { return { x: lastLocation.x + index, y: lastLocation.y }},
L: (index: number, lastLocation: Tlocation) => { return { x: lastLocation.x - index, y: lastLocation.y }},
D: (index: number, lastLocation: Tlocation) => { return { x: lastLocation.x, y: lastLocation.y - index }}
};
pathToPlot.forEach(points => {
// In JS I did it like this:
// const direction = points[0].match(/[a-zA-Z]/)[0];
// Typescript equivalent?
const matchLetter: RegExpMatchArray | null = points[0].match(/[a-zA-Z]/);
// This is giving errors:
const direction: Tdirection = matchLetter[0];
// two errors:
// 1. Object is possibly 'null'. ts(2531)
// 2. Type 'string' is not assignable to type 'Tdirection'. ts(2322)
console.log(operations[direction](1, { x: 0, y: 0 }));
});
我已经做了什么:
我阅读了几篇相关文章。
有建议称为RegExpArray定义新类型(在此处描述)。但是这似乎重新定义现有类型很奇怪。我宁愿使用现有的类型并围绕它执行自己的逻辑以使其通过。
我还阅读了文章"How to avoid null check pollution in Javascript: use Optionals" (文章)。它指出,您可以使用提供检查值是否为null的方法的库。如果是,则使用错误消息处理它,然后返回其他内容。
不使用库,有没有办法呢?