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

gentle-yak-16991

11/24/2023, 10:31 AM
using cli I can do
pulumi config set --path 'a.b.x' 1
and then
pulumi config get --path 'a.b.x'
which will print
1
. How would I do this in TypeScript? I cannot seem to find an example on how to fetch nested values. The first step is something like this
config.requireObject('a')
but as requireObject has a return type of unknown i cannot do dot notation or
x in y
notation.
c

clever-sunset-76585

11/25/2023, 6:10 AM
It's manual. You have to declare an interface in TS and assign the result to a variable of that interface type. Then you get the full type. It is not optimal but it is the (only?) way right now.
r

rhythmic-orange-74390

11/26/2023, 4:42 PM
in TS you can also cast unknown to an interface (as already mentioned), or
any
to achieve this
Copy code
> const x: unknown = { a: { b: { x: "foo" }}};
undefined
> console.log((x as { a: {b: {x: string; }}}).a.b.x)
foo
> console.log((x as any).a.b.x)
foo
creating an interface to represent your config is considered idiomatic typescript. Here interface A represents the structure of the “a” config key:
Copy code
interface A {
   b: {
      x: string;
   }
}

const myA = config.requireObject<A>("a");  // generic cast
const x = myA.b.x;