Hello, I'm attempting to use stack references with...
# general
k
Hello, I'm attempting to use stack references with Typescript and I'm having trouble figuring out the best way to get the stack output reference as the correct type. When I do a
requireOutput()
from the stack reference it always ends up as
Output<any>
type. Is there a way to set the expected type of the output so I can get something like
pulumi.Output<string>
or other types?
o
If you're quite confident, you can usually cast the type via
as Output<string>
, or if that doesn't work,
as any as Output<string>
. A helper library like this one contains type guards like "is.string". You'd need to use that inside an Apply and decide how to handle the else case - throwing an error in the Apply would work. https://www.npmjs.com/package/@sindresorhus/is
I think you'd end up with:
Copy code
requireOutput(...).apply(x => {
  if is.string(x) {
    return x;
  } else { 
    throw new Error("expected a string!")
});
k
Thanks @orange-policeman-59119, very helpful! For anyone that stumbles upon this, I also found you can do an await for stack references that aren't secrets. Makes it much easier to handle them since they end as default string types instead of pulumi output strings:
Copy code
export = async () => {
  const pulumiStackReference = new pulumi.StackReference(`otherStack`);
  const myOutput = await pulumiStackReference.requireOutputValue('myOutput')
}