I don't understand the document in the section <Re...
# help
p
I don't understand the document in the section Referencing SST stack outputs in other SST stacks It says in
stackA.js
Copy code
this.bucket = new s3.Bucket(this, "MyBucket");
And in
stackB.js
Copy code
// stackA's bucket is passed to stackB
const { bucket } = this.props;
// SST will implicitly set the exports in stackA
// and imports in stackB
bucket.bucketArn;
Is the
bucket
props passed automatically to stackB? Don't we have to do something like following?
Copy code
const stackA = new StackA(app, 'stack-a');
new StackB(app, 'stack-b', { bucket: stackA.bucket });
a
Pavan, you are correct. You can find a more explicit example in the guide here: https://serverless-stack.com/chapters/add-an-api-to-create-a-note.html
p
That make sense. But when I have following code, typescript typechecking throws error.
Copy code
new StackB(app, 'stack-b', { bucket: stackA.bucket });
Argument of type '{ bucket: string; }' is not assignable to parameter of type 'StackProps'.
f
Hey @Pavan Kumar, if you are using TypeScript, you’d need to define a Props that extends
StackProps
like this:
Copy code
interface StackBProps extends StackProps {
  readonly bucket: sst.Bucket;
}
You an find an example here of sharing an
sst.Api
across stack, but the idea is the same for sharing an
sst.Table
.
Another way @thdxr likes to do is to pass the bucket directly in the constructor like this:
Copy code
new StackB(app, 'stack-b', stackA.bucket);
And update `StackB`’s constructor to be:
Copy code
constructor(scope: App, id: string, bucket: sst.Bucket, props?: StackProps)
This way, you don’t need to create the new interface.
Let me know if this makes sense.
p
This way makes most sense.
Copy code
constructor(scope: App, id: string, bucket: sst.Bucket, props?: StackProps)
Why did the constructor had the third parameter
props?: StackProps
if it is usually something we pass. Dose it has some other purpose?
a
I find it is more handy when you are writing in pure js less so for ts
t
You don't need to pass stackprops if you're not overriding any of the settings in it
It's optional
p
What I meant is, if props is meant to send custom props (not strictly StackProps), then we should have different type instead of
StackProps
May be something like this.
Copy code
interface GenericProps {
   [key: string]: any;
}

type NewStackProps = StackProps && GenericProps
Then the Stack constructer would have following signature.
Copy code
new Stack(scope: Construct, id: string, props: NewStackProps)
t
I agree those types should be kept separate but there's many ways you can do this so comes down to personal preference