sst / ion

SST v3
https://sst.dev
MIT License
1.99k stars 231 forks source link

`sst.config.ts` TypeScript experience sensitive to `moduleResolution` setting #642

Open sockthedev opened 3 months ago

sockthedev commented 3 months ago

This is probably more of a note than anything else. Maybe some docs / defaults on the topic would do.

Steps to reproduce.

mkdir ion-module-resolution-sensitive
cd ion-module-resolution-sensitive
pnpm init
sst init

Add the following tsconfig:

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
  }
}

I also updated my package.json slightly, although I don't think this has any impact:

{
  "name": "ion-module-resolution-sensitive",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "sst": "ion"
  },
  "devDependencies": {
    "@types/aws-lambda": "8.10.140",
    "@types/node": "^20.14.9",
    "typescript": "5.5.3"
  }
}

Go into sst.config.ts. Types not working.

Make the following change to tsconfig:

 {
   "compilerOptions": {
     "module": "NodeNext",
-    "moduleResolution": "NodeNext",
+    "moduleResolution": "Bundler",
   }
 }

Now sst.config.ts types are working.


Why do I use "moduleResolution": "NodeNext"?

I find the "Bundler" setting results in an inconsistent experience, around the file extension being included, when auto resolving import statements. I'm trying to force my code to obey ESM rules to avoid any annoying refactors in the future. Took me a while to identify that as the culprit.

michaelgmcd commented 3 months ago

The issue is the imports in the ".sst/platform" folder don't have extensions. It could probably be solved with some sort of flag during sst init or an equivalent command, but it's not the easiest problem to solve with the current state of TS and how SST is set up.

oca159 commented 6 days ago

I wanted to check in on the status of this. I'm currently using NestJS with SST, and when I run pnpm run build, I'm encountering the following errors:

  node_modules/.pnpm/typescript@5.6.2/node_modules/typescript/lib/lib.es5.d.ts:517:14
    517     readonly length: number;
                     ~~~~~~
    'length' is declared here.
.sst/platform/src/components/aws/service.ts:137:5 - error TS2322: Type 'Output<any>' is not assignable to type 'Output<string>'.
  Type 'OutputInstance<any>' is not assignable to type 'Output<string>'.
    Property 'length' is missing in type 'OutputInstance<any>' but required in type 'LiftedObject<String, NonFunctionPropertyNames<String>>'.

137     this.domain = pub?.domain
        ~~~~~~~~~~~

  node_modules/.pnpm/typescript@5.6.2/node_modules/typescript/lib/lib.es5.d.ts:517:14
    517     readonly length: number;
                     ~~~~~~
    'length' is declared here.
.sst/platform/src/components/aws/vpc.ts:246:5 - error TS2322: Type 'Output<any>' is not assignable to type 'Output<Instance>'.
  Type 'OutputInstance<any>' is not assignable to type 'Output<Instance>'.
    Type 'OutputInstance<any>' is missing the following properties from type 'LiftedObject<Instance, NonFunctionPropertyNames<Instance>>': tags, id, urn, arn, and 55 more.

246     this.bastionInstance = output(bastionInstance);
        ~~~~~~~~~~~~~~~~~~~~

here is my tsconfig:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}
oca159 commented 6 days ago

i fixed the issue using the following tsconfig:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}