When using the node runtime with TypeScript, is th...
# typescript
r
When using the node runtime with TypeScript, is there some special handling concerning the TypeScript compiler flags (e.g.
noImplicitAny
?) I’m using some third-party javascript library which doesn’t provide good type definitions and as I’m only using two methods, I went with a custom
thelib.d.ts
file containing
declare module 'thelib';
This works with
noImplicitAny
in my IDE and on the command line but fails when it is handed over to the pulumi runtime. Not sure if this a Pulumi issue as I would somehow also expect it to fail locally. (i added my d.ts file to the files array and the typeRoots in tsconfig)
w
What error do you get? You should be able to set compilation flags in
tsconfig.json
. We just load
ts-node
into the Node process to compile-on-module-load
.ts
files. It should be reading the
tsconfig.json
to decide what TypeScript compilation flags to pass.
r
Error is TS7016 Could not find a declaration file for module ‘mssql’.
works when I do
npx tsc -p tsconfig.json
And to validate, it fails when I remove the content of the
mssql.d.ts
file and issue the same
tsc
command.
tsconfig:
Copy code
{
  "compileOnSave": true,
  "compilerOptions": {
    "strict": true,
    "outDir": "bin",
    "target": "es2016",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "experimentalDecorators": true,
    "pretty": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "forceConsistentCasingInFileNames": true,
    "typeRoots": [
      "node_modules/@types/",
      "./typings/mssql.d.ts"
    ]
  },
  "files": [
    "index.ts",
    "dummy.ts",
    "typings/mssql.d.ts",
    "mssql-tasks.ts",
    "mssql-serviceuser.ts"
  ]
}
Still wondering if a simple
declare module 'mssql';
should work if I’m using
noImplicitAny
as I’m somehow not defining the types explicitly.
There are also a bunch of typescript github issues related to that topic of
noImplicitAny
and those simple declarations. https://github.com/Microsoft/TypeScript/issues/15031
The long version of the error is:
Copy code
TSError: ⨯ Unable to compile TypeScript:
    mssql-tasks.ts(1,22): error TS7016: Could not find a declaration file for module 'mssql'. '/Users/ajaegle/dev/evals/pulumi-tests/testing-dynamic-provider/node_modules/mssql/index.js' implicitly has an 'any' type.
      Try `npm install @types/mssql` if it exists or add a new declaration (.d.ts) file containing `declare module 'mssql';
It looks like something weird when passing the ts/d.ts files to the runtime and then resulting in the noImplicitAny problem as the module was not declared anywhere.
All right. I somehow screwed up the
typeRoots
which must point to a directory similar to
node_modules/@types
.
With the correct folder structure of the typings folder
typings/<module-name>/<jsfilename>.d.ts
and the following
typeRoots
it works:
Copy code
"typeRoots": [
    "node_modules/@types/",
    "./typings/"
]
What’s the best place to add this to the docs? Is there some JS/TS specific documentation?
184 Views