polite-napkin-90098
08/03/2023, 4:19 PMlet numTasks = vpc.publicSubnets.apply(subnets => subnets.length);
// that will work when we only have 1 subnet in the Test-vpc but for now
if (nom === 'test'){
numTasks = 1;
}
This gives the error:
stack/service.ts(14,3): error TS2322: Type '1' is not assignable to type 'Output<any>'.
from the line where I try and force it to be 1 inside the if
I've tried
let numTasks: number = vpc.publicSubnets.apply(subnets => subnets.length);
making it a number in the declaration but that then errors with:
stack/service.ts(9,5): error TS2322: Type 'Output<any>' is not assignable to type 'number'.
I'm pretty sure my code is right and that on execution subnets.length will be a number, but the typing doesn't see what is obvious to me.
Anyone got any pointers on how to fix this?clever-sunset-76585
08/03/2023, 4:35 PMlet numTasks = vpc.publicSubnets.apply(subnets => subnets.length);
// that will work when we only have 1 subnet in the Test-vpc but for now
if (nom === 'test'){
numTasks = 1;
}
When you don't define an explicit type for numTasks
, the compiler sees that its type is the result of the apply()
which is Output<T>
but then later when you try to re-assign that to a number
, it's an error.
let numTasks: number = vpc.publicSubnets.apply(subnets => subnets.length);
Here you are explicitly defining the type to be a number
which is not correct because you are defining one type but then assigning a value of a different type, Output<T>
.
TypeScript supports union types, which allows you to do:
let numTasks: number | Output<number> = vpc.publicSubnets.apply(subnets => subnets.length);
BUT you have another option. Pulumi has a built-in type called Input<T>
which is a union of similar to the above. It's what all resource's properties are and it's why you can either give them plain values or promise-like values.
let numTasks: Input<number> = vpc.publicSubnets.apply(subnets => subnets.length);
full-eve-52536
08/03/2023, 4:35 PMnumTasks
can either be of type `Output<any>`(Which is a Pulumi-specific type), or it can be of type number
. I think what you want is:
let numTasks: number | Output<number>
polite-napkin-90098
08/03/2023, 5:29 PMlittle-cartoon-10569
08/03/2023, 8:41 PMpulumi.Input<number>
.pulumi.Output<number>
and change the if-clause to
if (nom === 'test'){
numTasks = pulumi.output(1);
}
full-eve-52536
08/03/2023, 8:42 PMpulumi.Output()
as a function? Hmm..didn't know that. TILlittle-cartoon-10569
08/03/2023, 8:43 PMpulumi.Output
is a type. pulumi.output()
is a function.pulumi.Output
.