https://pulumi.com logo
#typescript
Title
# typescript
p

prehistoric-kite-30979

11/12/2021, 12:54 PM
Hi all is there an easy was to convert
Output<Object<string>>
to
Object<Input<string>>
without having to explicitly list all the fields?
h

happy-parrot-60128

11/12/2021, 1:14 PM
Is
Object
your own type in your example, something like this but with more fields?
Copy code
interface Object<T> { field: T }
Also, can you give more detail as to what's requiring the input to be in the latter form? Often input types are nested to give you flexibility on which way round your input's are structured e.g.
Copy code
type ExampleInput = pulumi.Input<{
  field: pulumi.Input<string>;
}>
p

prehistoric-kite-30979

11/12/2021, 1:37 PM
Some background: We have an extended ResourceOptions library that helps us explicitly require that a provider be for azure/aws, etc. It also does other things but lets keep it simple for now… I’m trying to write some helper functions that take args and build out these options so that we can pass stack references outputs to them.
Copy code
export function AzureMinimal(
    name: string,
    args: MinimalOptionsArgs<pulumi.Input<string>>,
    opts?: ProviderOptions,
): AzureMinimalOptions {
    const provider = new azure.Provider(
        name,
        {
            subscriptionId: args.subscriptionId,
            tenantId: args.tenantId,
        },
        opts,
    )
    return {
        ...args,
        provider,
    }
}
This works except when calling it I have to explicitly pass fields… Works…
Copy code
options.AzureMinimal('azure', {
    subscriptionId: cloud.metadata.subscriptionId,
    tenantId: 'some-uuid,
}),
Doesnt work…
Copy code
options.AzureMinimal('azure', {
    ...cloud.metadata,
    tenantId: 'some-uuid,
}),
Copy code
interface MinimalOptionsArgs<StringOrIntputString> extends CustomResourceOptions {
    subscriptionId: StringOrIntputString
    tenantId: StringOrIntputString
}
(This is obviously fine for minimal opts but some get a bit larger)
I basically don’t want the caller to have to list out all the fields, I want to do it within the function.
apologies that this is a bit muddled…
the latter passes through an undefined subscription + tenant id
h

happy-parrot-60128

11/12/2021, 2:23 PM
Ah - so the Typescript compiler is happy, but it fails with an undefined value at runtime?
p

prehistoric-kite-30979

11/12/2021, 2:23 PM
yes
h

happy-parrot-60128

11/12/2021, 2:27 PM
Assuming
cloud.metadata
is an output of your desired fields, could you try doing the following?
Copy code
options.AzureMinimal('azure', cloud.metadata.apply(metadata => ({
    ...metadata,
    tenantId: 'some-uuid,
}))),
p

prehistoric-kite-30979

11/12/2021, 2:30 PM
that works 🙂
thanks
I’ll try to add some defensive coding in the function to protect from undefineds
h

happy-parrot-60128

11/12/2021, 2:58 PM
Great! Happy to help ☺️