我试图允许超类中的方法返回子类的实例,以便我可以使用方法链接与父和子的方法.
但是,当我尝试链接方法时,我收到错误"BaseClass没有名为someOtherChainableMethod的成员".这是我的代码:
class BaseClass {
func someChainableMethod() -> BaseClass {
return self
}
}
class ChildClass: BaseClass {
func someOtherChainableMethod() -> ChildClass {
return self
}
}
let childClass = ChildClass
childClass.someChainableMethod().someOtherChainableMethoid()
Run Code Online (Sandbox Code Playgroud)
问题似乎是父链式方法中的"返回自我"返回一个类型BaseClass而不是类型的实例ChildClass.
我也用泛型尝试了这个并且失败了,这就是我尝试过的:
class BaseClass<T> {
func someChainableMethod() -> T {
return self
}
}
class ChildClass: BaseClass<ChildClass> {
func someOtherChainableMethod() -> ChildClass {
return self
}
}
let childClass = ChildClass
childClass.someChainableMethod().someOtherChainableMethoid()
Run Code Online (Sandbox Code Playgroud)
在这种情况下,BaseClass someChainableMethod方法的错误是"BaseClass不能转换为T".
我试图在构建 Lambda 函数时使用 TypeScript,但在使用同样用 TypeScript 编写的 Lambda 层时遇到问题。
TypeScript 无法识别我的层的 /opt/nodejs/... 导入(因为它会在 SAM 或 AWS 中运行),因此我无法将我在层中定义的类型导入到我的 Lambda 函数。
我试图查看我是否能够以某种方式 npm 链接该层,但我似乎无法使其正常工作,因为导入是本地路径 '/opt/nodejs...' 而不仅仅是模块名称。
层:
export interface SomeType {
someField: string
}
Run Code Online (Sandbox Code Playgroud)
拉姆达:
import { SomeType } from '/opt/nodejs/myLayer' // this does not work
Run Code Online (Sandbox Code Playgroud)
我刚刚收到错误消息:Cannot find module '/opt/nodejs/myLayer'.ts(2307)我发现自己不得不用它来抑制它,// @ts-ignore can be ignored as this is a Lambda layer但这意味着我无法使用层中的 TypeScript 类型。
我一直在尝试使用 CloudFormation 部署到 API Gateway,但是,我的方法资源经常遇到同样的问题。堆栈部署不断失败,并显示“指定的资源标识符无效”。
这是我的 CloudFormation 模板中的方法资源:
"UsersPut": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"ResourceId": "UsersResource",
"RestApiId": "MyApi",
"ApiKeyRequired": true,
"AuthorizationType": "NONE",
"HttpMethod": "PUT",
"Integration": {
"Type": "AWS_PROXY",
"IntegrationHttpMethod": "POST",
"Uri": {
"Fn::Join": ["", ["arn:aws:apigateway:", {
"Ref": "AWS::Region"
}, ":lambda:path/2015-03-31/functions/", {
"Fn::GetAtt": ["MyLambdaFunc", "Arn"]
}, "/invocations"]]
}
},
"MethodResponses": [{
"StatusCode": 200
}]
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人能帮我弄清楚为什么这会导致堆栈部署失败?
更新:我忘了提到我还尝试使用引用来添加资源 ID,这也给了我同样的错误:
"UsersPut": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"ResourceId": {
"Ref": "UsersResource"
},
"RestApiId": "MyApi",
"ApiKeyRequired": true,
"AuthorizationType": "NONE",
"HttpMethod": "PUT",
"Integration": {
"Type": …Run Code Online (Sandbox Code Playgroud)