我最近开始使用打字稿,但遇到错误,我不知道如何解决。这是我的功能:
interface Arr<E extends Runtype> extends Runtype<Static<E>[]> {
tag: 'array';
element: E;
}
/**
* Construct an array runtype from a runtype for its elements.
*/
function Arr<E extends Runtype>(element: E): Arr<E> {
return create<Arr<E>>(
xs => {
if (!Array.isArray(xs)) throw new ValidationError(`Expected array, but was ${typeof xs}`);
let newArray:Array<E> = new Array();
for (const x of Array.from(xs)) {
try {
newArray.push(<E>element.check(x));
} catch ({ message, key }) {
throw new ValidationError(
message,
key ? `[${xs.indexOf(x)}].${key}` : `[${xs.indexOf(x)}]`,
)
}
}
return newArray;
},
{ tag: 'array', element },
)
}
export { Arr as Array };
Tsconfig:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"removeComments": false,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "esnext"
}
}
非常感谢您对这里出了什么问题有所帮助。...我已尝试切换目标环境和内容
更新:
另一个类似的错误:
export interface Record<O extends { [_: string]: Runtype }>
extends Runtype<{ [K in keyof O]: Static<O[K]> }> {
tag: 'record';
fields: O;
}
/**
* Construct a record runtype from runtypes for its values.
*/
export function Record<O extends { [_: string]: Runtype }>(fields: O) {
return create<Record<O>>(
x => {
if (x === null || x === undefined) {
const a = create<any>(x, { tag: 'record', fields });
throw new ValidationError(`Expected ${show(a)}, but was ${x}`);
}
let y:any = {};
// tslint:disable-next-line:forin
for (const key in fields) {
try {
const keyStr:string = <string>key;
let val = fields[key].check(hasKey(key, x) ? x[key] : undefined);
y[<string>key] = <any>val;
} catch ({ key: nestedKey, message }) {
console.log('key ',key,' ',message);
throw new ValidationError(message, nestedKey ? `${key}.${nestedKey}` : key);
}
}
return y as O;
},
{ tag: 'record', fields },
);
}
任何人都知道这些示例出了什么问题,使打字稿对此有所反应?
答案 0 :(得分:-1)
您可以尝试更改此内容
newArray.push(<E>element.check(x));
使用
newArray.push(<E>element.check(x)</E>);
由于我没有您的完整申请,因此我不确定这是否会100%有效。但这更改为我解决了许多括号。