我可以从Lambda图层导入打字稿类型吗? [英] Can I import typescript types from a Lambda Layer?
问题描述
我在构建Lambda函数时尝试使用TypeScript,但是在使用也是用TypeScript编写的Lambda Layer时遇到问题.
I am attempting to use TypeScript when building Lambda functions but hitting an issue when using a Lambda Layer which is also written in TypeScript.
TypeScript无法识别我的Layer的/opt/nodejs/...导入(因为它将在SAM或AWS中运行),因此我无法将在Layer中定义的类型导入到Lambda函数中.
TypeScript does not recognise the /opt/nodejs/... import for my Layer (as it would running in SAM or AWS) and therefore I am unable to import types which I have defined in the Layer to my Lambda function.
我试图查看我是否能够以某种方式npm链接图层,但似乎无法正常工作,因为导入是本地路径"/opt/nodejs ...",而不仅仅是一个模块名称.
I have tried looking to see if I am able to somehow npm link the layer but I can't seem to get that working because the import is a local path '/opt/nodejs...' and not a just a module name.
层:
export interface SomeType {
someField: string
}
Lambda:
import { SomeType } from '/opt/nodejs/myLayer' // this does not work
我只是得到错误:Cannot find module '/opt/nodejs/myLayer'.ts(2307)
,我发现自己必须用// @ts-ignore can be ignored as this is a Lambda layer
抑制此错误,但这意味着我无法使用该图层中的TypeScript类型.
I am just getting the error: Cannot find module '/opt/nodejs/myLayer'.ts(2307)
and I find myself having to suppress this with // @ts-ignore can be ignored as this is a Lambda layer
but that means I am unable to use the TypeScript types from the layer.
推荐答案
我想我可能已经解决了这个问题,我在tsconfig.json中添加了/opt/nodejs ...路径作为路径映射:
I think I may have solved this one, I added the /opt/nodejs... path as a path mapping in tsconfig.json:
{
"paths": {
"/opt/nodejs/myLayer": ["../../layers/myLayer/src/some-layer-module"]
}
}
我现在可以从图层中导入TypeScript类型,并且由于导入路径没有更改,它仍然可以在AWS/SAM中使用
I can now import TypeScript types from the Layer and as the import path isn't changed, it will still work in AWS/SAM
这篇关于我可以从Lambda图层导入打字稿类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!