semantic error TS2574: A rest element type must be an array type

222 views Asked by At

Here is the code src/object.ts(50,58)

interface sanitizer<T> {
  (val: T): T
}

type MapSanitizor<T extends any[]> = T extends [infer P, ...(infer R)]
  ? [sanitizer<P>, ...MapSanitizor<R>]
  : [];

function isArrayAs<T extends any[]>(fn: MapSanitizor<T>): sanitizer<T> {
  return function(val) {
    if (!Array.isArray(val))
      throw new Error(`
        obj val \n
        = ${JSON.stringify(val)} \n
        is invalid value,\n
        value passed in isArrayAs is not an array
      `);
    const newlist: T = [] as any;
    for (let i = 0; i < fn.length; i++) {
      newlist.push((fn[i] as any)(val[i]));
    }
    return newlist;
  };
}

I am trying to build my package it given me this error when i run npm run build

Error: D:/packages/npm/sanitize-json/src/object.ts(50,58): semantic error TS2574: A rest element type must be an array type.
    at error (D:\packages\npm\sanitize-json\node_modules\rollup\dist\shared\node-entry.js:5400:30)
    at throwPluginError (D:\packages\npm\sanitize-json\node_modules\rollup\dist\shared\node-entry.js:11878:12)
    at Object.error (D:\packages\npm\sanitize-json\node_modules\rollup\dist\shared\node-entry.js:12912:24)
    at Object.error (D:\packages\npm\sanitize-json\node_modules\rollup\dist\shared\node-entry.js:12081:38)
    at RollupContext.error (D:\packages\npm\sanitize-json\node_modules\rollup-plugin-typescript2\dist\rollup-plugin-typescript2.cjs.js:17237:30)
    at D:\packages\npm\sanitize-json\node_modules\rollup-plugin-typescript2\dist\rollup-plugin-typescript2.cjs.js:25033:23
    at arrayEach (D:\packages\npm\sanitize-json\node_modules\rollup-plugin-typescript2\dist\rollup-plugin-typescript2.cjs.js:545:11)
    at Function.forEach (D:\packages\npm\sanitize-json\node_modules\rollup-plugin-typescript2\dist\rollup-plugin-typescript2.cjs.js:9397:14)
    at printDiagnostics (D:\packages\npm\sanitize-json\node_modules\rollup-plugin-typescript2\dist\rollup-plugin-typescript2.cjs.js:25006:12)
    at Object.transform (D:\packages\npm\sanitize-json\node_modules\rollup-plugin-typescript2\dist\rollup-plugin-typescript2.cjs.js:29277:17)

what have i done wrong? i dont see any error popup in vscode compiler.

0

There are 0 answers