Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 如何在Typescript中优雅地导入AWS-Lambda?_Amazon Web Services_Typescript_Types_Aws Lambda - Fatal编程技术网

Amazon web services 如何在Typescript中优雅地导入AWS-Lambda?

Amazon web services 如何在Typescript中优雅地导入AWS-Lambda?,amazon-web-services,typescript,types,aws-lambda,Amazon Web Services,Typescript,Types,Aws Lambda,我正在aws lambda上构建一个typescript项目。由于aws sdk已经提供了类型定义,我希望它也包含aws lambda的定义。但我似乎必须分别安装@types/aws lambda,才能让它工作 //import { Lambda } from "aws-sdk"; import { Context } from "aws-lambda"; module.exports.hello = async (event:any, context:Context) => { r

我正在aws lambda上构建一个typescript项目。由于
aws sdk
已经提供了类型定义,我希望它也包含aws lambda的定义。但我似乎必须分别安装
@types/aws lambda
,才能让它工作

//import { Lambda } from "aws-sdk";
import { Context } from "aws-lambda";

module.exports.hello = async (event:any, context:Context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'function executed successfully!',
      input: event,
    }),
  };
};
我希望这样的事情是可能的:

import { Lambda } from "aws-sdk";

module.exports.hello = async (event:any, context:Lambda.Context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'function executed successfully!',
      input: event,
    }),
  };
};
但事实并非如此;)


那么,如何正确执行呢?

aws sdk不包含lambda的类型。不幸的是,您将同时需要
aws sdk
@types/aws lambda
。另外,我建议在
包.json的
devDependencies
中声明
@types/aws lambda

import * as AWS from "aws-sdk";
import { Context } from "aws-lambda";

module.exports.hello = async (event:any, context:Context) => {
  // eg. if you need a DynamoDB client
  // const docClient: AWS.DynamoDB.DocumentClient = new AWS.DynamoDB.DocumentClient({region: 'ap-southeast-2'});
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'function executed successfully!',
      input: event,
    }),
  };
};

你看过aws sdk的源代码了吗?我认为没有理由期望Lambda函数中相关的结构也与SDK相关。它们可能就在那里,但我不明白为什么它们会有用,因为SDK的交互面临着错误的方向(因为没有更好的术语)。