Issue
I am trying to learn TypeScript and CDKV2.
test.ts
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { teststack } from '../src/teststack';
const queueName = 'input-queue';
const app = new cdk.App();
new Task2Stack(app, 'teststack', { queueName: queueName });
teststack.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sqs from 'aws-cdk-lib/aws-sqs';
export interface getStackProps extends cdk.StackProps {
readonly queueName: string;
env: {
account: '123456789012',
region: 'us-east-1'
}
}
export class teststack extends cdk.Stack {
constructor(scope: Construct, id: string, props: getStackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// example resource
const queue = new sqs.Queue(this, '.....', {
visibilityTimeout: cdk.Duration.seconds(300),
queueName: props.queueName,
});
}
}
Error:
Argument of type '{ queueName: string; }' is not assignable to parameter of type 'getStackProps'.
Property 'env' is missing in type '{ queueName: string; }' but required in type 'getStackProps'.ts(2345)
teststack.ts(7, 3): 'env' is declared here.
Question: If I remove/comment env it works fine. Is it possible to convert getStackProps to a function so that I can eliminate this error ? What is causing this error ? Can anyone please explain ?
Solution
What is causing this error ?
The error message says it all: the stack constructor requires an object of type getStackProps
* as the 3rd argument, but you are not providing one. It's missing the required env
.
Can anyone please explain ?
account: '123456789012'
does not define a default account. Rather, it's a very narrow type parameter. The account must be 123456789012
. Any other value will cause a compiler error. Here's an illustration (TS Playground) where I try to pass a value other than us-east-1
to region
. As expected, the TS compiler complains:
// type definition - what is allowed?
interface TestStackProps {
queueName: string;
env: {
account: '123456789012',
region: 'us-east-1'
}
}
// OK
const goodProps: TestStackProps = {
queueName: "my-queue-name",
env: {
account: '123456789012',
region: 'us-east-1'
}
}
// compiler error!
// Type '"mars-region"' is not assignable to type '"us-east-1"'
const badProps: TestStackProps = {
queueName: "my-queue-name",
env: {
account: '123456789012',
region: 'mars-region'
}
}
Is it possible to convert getStackProps to a function so that I can eliminate this error ?
No. Not the right approach.
* By convention TS capitalizes type names and class names. teststack -> TestStack
, getStackProps -> TestStackProps
.
Answered By - fedonev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.