typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Use of import attributes triggers an error incorrectly

Open wojtekmaj opened this issue 6 months ago • 3 comments

Use of import attributes triggers an error in tsgo (7.0.0-dev.20250528.1), while the latest vintage 😁 TypeScript (5.8.3) does not.

tsconfig.json

{
  "compilerOptions": {
    "isolatedModules": true,
    "lib": ["es2023"],
    "module": "nodenext",
    "moduleDetection": "force",
    "noEmit": true,
    "noUncheckedIndexedAccess": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "es2022",
    "verbatimModuleSyntax": true
  }
}

Error:

src/(redacted).ts:3:49 - error TS2856: Import attributes are not allowed on statements that compile to
CommonJS 'require' calls.

3 import redacted from '../data/redacted.json' with { type: 'json' };

Out of curiosity, I changed module to node16 to see if it makes any difference. To my surprise, I got:

src/(redacted).ts:3:49 - error TS2823: Import attributes are only supported when the '--module' option
is set to 'esnext', 'nodenext', or 'preserve'.

...so it suggests me to set module to what it originally was to get rid of the error!

wojtekmaj avatar May 28 '25 21:05 wojtekmaj

Do you have a repo we could test?

jakebailey avatar May 29 '25 06:05 jakebailey

Sure! Here's a minimal repro.

repro.zip

wojciech.maj@MacBook-Pro repro % npm i

added 9 packages, and audited 10 packages in 631ms

found 0 vulnerabilities
wojciech.maj@MacBook-Pro repro % node --run test:strada
wojciech.maj@MacBook-Pro repro % node --run test:corsa 
src/index.ts:1:32 - error TS2856: Import attributes are not allowed on statements that compile to CommonJS 'require' calls.

1 import data from './data.json' with { type: 'json' };
                                 ~~~~~~~~~~~~~~~~~~~~~

Found 1 error in src/index.ts:1

wojtekmaj avatar May 29 '25 09:05 wojtekmaj

Setting module to preserve worked to avoid the inconsistent error, though I'm only using tsgo, with separate config, strictly for type checking at the momment; tsc is still used for building.

nf-wbeck avatar Jun 02 '25 21:06 nf-wbeck

Can't reproduce it with the recent version:

PS C:\server> yarn tsc
src/(redacted).ts:3:49 - error TS2856: Import attributes are not allowed on statements that compile to
CommonJS 'require' calls.

3 import redacted from '../data/redacted.json' with { type: 'json' };
                                               ~~~~~~~~~~~~~~~~~~~~~

Found 1 errors in 1 file.

Errors  Files
     2  src/(redacted).ts:3

PS C:\server> yarn up @typescript/native-preview -R
➤ YN0000: · Yarn 4.9.1
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + @typescript/native-preview@npm:7.0.0-dev.20250619.1, and 7 more.
➤ YN0085: │ - @typescript/native-preview-darwin-arm64@npm:7.0.0-dev.20250528.1, and 7 more.
➤ YN0000: └ Completed in 1s 967ms
➤ YN0000: ┌ Fetch step
➤ YN0013: │ 7 packages were added to the project, and 7 were removed (+ 11.24 MiB).
➤ YN0000: └ Completed in 2s 268ms
➤ YN0000: ┌ Link step
➤ YN0008: │ server@workspace:. must be rebuilt because its dependency tree changed
➤ YN0000: └ Completed in 1s 283ms
➤ YN0000: · Done in 5s 610ms
PS C:\server> yarn tsc
PS C:\server>

wojtekmaj avatar Jun 19 '25 20:06 wojtekmaj

Yes, this was fixed in #1070.

jakebailey avatar Jun 20 '25 16:06 jakebailey