Hi! I’m importing a library that doesn’t have modu...
# typescript
m
Hi! I’m importing a library that doesn’t have module declarations. I created a
types/index.d.ts
file, and I added it to
tsconfig.json
. Here is the file:
Copy code
{
  "compilerOptions": {
    "strict": true,
    "outDir": "bin",
    "target": "es2016",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "experimentalDecorators": true,
    "pretty": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true
  },
  "include": [
    "infra",
    "types"
  ],
  "exclude": [
    "node_modules",
    "vendor",
    "dist"
  ]
}
I have the impression that Pulumi is ignoring my config. Does anybody see what may be wrong on my setup? VSCode’s TS server finds the
.d.ts
declaration and doesn’t show the error I get when running
pulumi up
.
c
How have you declared the module in
types/index.d.ts
?
m
This is the code:
Copy code
declare module "@bref.sh/layers" {
	function fpmLayerArn(
		region: string,
		version: string,
		architecture?: string,
	): string;
}
Nah, I tried
compilerOptions.rootDirs
and
compilerOptions.typeRoots
and still erroring 😕
Also, forced the env variables using:
PULUMI_NODEJS_TYPESCRIPT=true PULUMI_NODEJS_TSCONFIG_PATH=tsconfig.json pulumi up
and it doesn’t work
It is strange, it follows some rules though, like
strict
and
noImplicitAny
😕
c
I think the issue is with the module you've declared in
types/index.d.ts
and not your TS configuration. Also I see that the original source contains JSDoc for that function which should provide the type hints that your module is trying to provide. Are you simply trying to add type-safety for that function or trying to fix some other problem?
m
No, i’m just trying to import it, and TS fails because it doesn’t have a types declaration
Copy code
import { fpmLayerArn } from "@bref.sh/layers";
// ...
export const wordpressServer = new aws.lambda.Function(
	"wordpress-server",
	{
		role: wordpressServerRole.arn,
		runtime: "provided.al2",
		layers: [fpmLayerArn("us-east-1", "8.2")],
		s3Bucket: wordpressBucket.bucket,
		s3Key: wordpressCode.key,
		handler: "lambda-handler.php",
		memorySize: 1024,
		sourceCodeHash: wordpressCode.sourceHash.toString(),
	},
	{
		dependsOn: [wordpressServerRole, wordpressBucket, wordpressCode],
	},
);
this is how I’m using it
I think I’ll go with
noImplicitAny
for now. I’ll try to look into this in the future though. Thanks for helping me out here!
c
So I was wrong about it not having to do with your
tsconfig.json
file. Your module declaration is indeed correct. You might try
include: ["***/**.ts"]
in your config.
r
I just ran into this issue. See https://github.com/TypeStrong/ts-node#missing-types for more details. but the TL;DR is to put your types in a structure like
Copy code
<project_root>/
-- tsconfig.json
-- typings/
  -- <module_name>/
    -- index.d.ts
and then in your
tsconfig.json
add the
typings
dir to the
typeRoots
list
Copy code
{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types",
      "./typings"
    ]
  }
}