Search code examples
typescripttsconfigtsc

Ignore TypeScript errors under all circumstances


How can I always ignore a TypeScript error without getting an error if the actual error does not occur?

Background is this snippet:

  const render = await (async () => {
    if (viteDevServer) {
      return (await viteDevServer.ssrLoadModule('./src/entryServer.ts')).render
    }

    // @ts-expect-error File may not be compiled in development
    return (await import('./dist/server/entry/entryServer.js')).render
  })()

With @ts-expect-error an error occurs if the file exists. Without @ts-expect-error an error occurs if the file does not exist.


Solution

  • use // @ts-ignore instead.

     const render = await (async () => {
      if (viteDevServer) {
        return (await viteDevServer.ssrLoadModule('./src/entryServer.ts')).render
      }
    
      // @ts-ignore
      return (await import('./dist/server/entry/entryServer.js')).render
    })()
    

    but please keep in mind that you should do it sparingly (not at all in best case) since it can hide real issues in your code.