serverless-plugin-typescript
serverless-plugin-typescript copied to clipboard
Doesn't include declaration file (.d.ts) while compilation
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 :)
+1
has any maintainer looked into this issue? because this is very important one.
Definitely a problem for me!
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"
],
...
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"
],
What also works is using old-school <reference>
comments:
/// <reference path='../types/module.d.ts' />
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.
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"
],
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 besrc/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:
I encountered this issue today. None of the fixed I found here or elsewhere worked. I've tried:
- using
typeRoots
andtypes
- specifically referencing my
index.d.ts
infiles
,include
, and both - Making a
@types
folder in/src
with apackage.json
withtypes
set toindex.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.
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