jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.22k stars 507 forks source link

TypeScript mapped types key remapping with `as` is not working -- requires TS 4.1 #988

Closed migueloller closed 3 years ago

migueloller commented 3 years ago

Current Behavior

tsdx build throws an error if attempting to build while using this TypeScript feature:

(typescript) Error: /Users/miguel/Code/cosmos/packages/builder/src/reducer/layouts.ts(9,51): syntax error TS1005: ']' expected.
Error: /Users/miguel/Code/cosmos/packages/builder/src/reducer/layouts.ts(9,51): syntax error TS1005: ']' expected.
    at error (/Users/miguel/Code/cosmos/node_modules/rollup/dist/shared/node-entry.js:5400:30)
    at throwPluginError (/Users/miguel/Code/cosmos/node_modules/rollup/dist/shared/node-entry.js:11878:12)
    at Object.error (/Users/miguel/Code/cosmos/node_modules/rollup/dist/shared/node-entry.js:12912:24)
    at Object.error (/Users/miguel/Code/cosmos/node_modules/rollup/dist/shared/node-entry.js:12081:38)
    at RollupContext.error (/Users/miguel/Code/cosmos/node_modules/tsdx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:17237:30)
    at /Users/miguel/Code/cosmos/node_modules/tsdx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:25033:23
    at arrayEach (/Users/miguel/Code/cosmos/node_modules/tsdx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:545:11)
    at Function.forEach (/Users/miguel/Code/cosmos/node_modules/tsdx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:9397:14)
    at printDiagnostics (/Users/miguel/Code/cosmos/node_modules/tsdx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:25006:12)
    at Object.transform (/Users/miguel/Code/cosmos/node_modules/tsdx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:29277:17)

This is the LOC where it breaks:

export type LayoutElements = { [K in keyof Layout as `${K}Element`]: Element | null }

Expected behavior

It should build just fine.

Suggested solution(s)

Perhaps an upgrade to rollup-plugin-typescript2 or using @rollup/plugin-typescript could help here?

Additional context

https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#key-remapping-via-as

Your environment

  System:
    OS: macOS 11.2.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 520.93 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.10.0 - ~/.nvm/versions/node/v15.10.0/bin/node
    Yarn: 1.22.10 - ~/Code/cosmos/node_modules/.bin/yarn
    npm: 7.5.3 - ~/.nvm/versions/node/v15.10.0/bin/npm
  Browsers:
    Chrome: 88.0.4324.192
    Firefox: 82.0
    Safari: 14.0.3
agilgur5 commented 3 years ago

Per the TS docs you later linked, this requires TypeScript 4.1+. TSDX is still on TS 3.x.

Please see #926 for a workaround, will be marking as duplicate


Also, with regard to the issue template:

Your environment

You're missing the most important piece of the output here -- as specified in the command, the versions of TS and TSDX should be shown. You probably ran this outside of your project directory -- the directions say to run inside of it.