serverless-plugin-typescript icon indicating copy to clipboard operation
serverless-plugin-typescript copied to clipboard

Doesn't include declaration file (.d.ts) while compilation

Open rokoroku opened this issue 6 years ago • 11 comments

When I have project-scoped declaration file(*.d.ts) in my project, packaging with serverless-plugin-typescript emits compile error like below:

Cannot find name 'TypeInterfaceName'.

(But regardless of the error, packaging is completed successfully and proceed to deployment process.)

So currently I have to write all the declaration files to normal typescript file, and import them to every functions now.

It'd be better to support them :)

rokoroku avatar Mar 08 '18 06:03 rokoroku

+1

TakaGoto avatar Mar 20 '18 15:03 TakaGoto

has any maintainer looked into this issue? because this is very important one.

chanlito avatar Mar 24 '18 04:03 chanlito

Definitely a problem for me!

justinwaite avatar Apr 30 '18 18:04 justinwaite

Also hit this one. My workaround is to include the declaration in the tsconfig.json types property. Path seems to need to be relative to the build folder.

e.g. with a declaration defined in ./src/types/serverless-http.d.ts specify:

...
"types": [ 
  "../src/types/serverless-http"
],
...

mlev avatar Jun 25 '18 01:06 mlev

My original workaround caused a few issues with my IDE. But I found if I package my local declarations differently I could use typeRoots in tsconfig.json instead.

e.g. Originally I had defined my types as a single file src/types/serverless-http.d.ts. Instead I repackaged it to be src/types/serverless-http/index.d.ts.

And changed my tsconfig.json

"typeRoots": [
  "node_modules/@types",
  "src/types"
],

mlev avatar Jun 28 '18 10:06 mlev

What also works is using old-school <reference> comments:

/// <reference path='../types/module.d.ts' />

denisw avatar Jul 04 '18 13:07 denisw

Thanks for the workarounds. Does anyone know if this is caused by the fact that this plugin is locked into typescript 2 rather than 3? Cause I saw a separate issue about that and also had noticed some stuff online suggesting the handling of types declarations had been improved.

eboswort avatar Nov 12 '18 22:11 eboswort

I hit this one as well, but struggled with VS Code not recognizing the definitions if I defined the path as "../external-type-definition" (relative to the build folder). My solution was to define the following in tsconfig.json to satisfy both this plugin and vscode:

"typeRoots": [
  "./",
  ".build/"
],                       
"types": [
  "../external-types/serverless-http"
],

afefer avatar Jan 03 '19 01:01 afefer

My original workaround caused a few issues with my IDE. But I found if I package my local declarations differently I could use typeRoots in tsconfig.json instead.

e.g. Originally I had defined my types as a single file src/types/serverless-http.d.ts. Instead I repackaged it to be src/types/serverless-http/index.d.ts.

And changed my tsconfig.json

"typeRoots": [
  "node_modules/@types",
  "src/types"
],

Only this worked for me.

I had a d.ts file with a name like: myservice.gql.d.ts in a types folder, but nothing worked. Then I just renamed the path to: myservice/index.d.ts, and voila! ([email protected], [email protected], [email protected])

I can't really wrap my head around what makes the plugin ignore the d.ts files, as my IDE worked fine, and when I ran ts compilation directly (npx tsc --noEmit) that picked up the d.ts file too without the whole index.d.ts rework. :man_shrugging:

ZeeCoder avatar Jun 22 '22 09:06 ZeeCoder

I encountered this issue today. None of the fixed I found here or elsewhere worked. I've tried:

  • using typeRoots and types
  • specifically referencing my index.d.ts in files, include, and both
  • Making a @types folder in /src with a package.json with types set to index.d.ts
  • Putting index.d.ts at various levels in the project

Like others have observed, the project does compile and IDE intellisense does work. I can successfully run tsc, use ts-node to run the .ts files, run mocha with ts-node...but I would like to understand and fix why I get Cannot find name 'X' when I use the Serverless package, deploy, and invoke-local functions.

RyanWhipple avatar Jan 25 '23 19:01 RyanWhipple

I'm also annoyed by this. I have an index.d.ts in my project and a functional workaround seems to be to add this as the first line in all my .ts files (might need a different relative path if your files are not all in the same dir with the type file):

/// <reference path='index.d.ts' /> workaround for https://github.com/serverless/serverless-plugin-typescript/issues/92

Haprog avatar Feb 23 '23 12:02 Haprog