#How can you infer types from for loop values based on keys?

92 messages · Page 1 of 1 (latest)

pliant plumeBOT
#
The Digital#3999

Preview:```ts
type objType = {
value1: string
number1: number
yes: string
}

const obj: objType = {
value1: "asfdasf",
number1: 912,
yes: "yes",
}

const newObj: objType = {
value1: "jkdfnvčleoknfgeokrg",
number1: 908543,
yes: "no",
...```

high pine
#

you'd need to have the iterated thing have tuples with specific keys, not an array of all the k/vs there

#

you could use a mapped type for this

#

why do you need to though?

#

seems like either
a different structure would be better
or
you already know the shape of your objects, so a loop isn't really necessary

dense nimbus
high pine
#

checking "each/most" properties
uh, what?

#

im confused

dense nimbus
#

most of values*

high pine
#

from your playground, you know the shape though

dense nimbus
#

i do but i would need types inferred because

dense nimbus
high pine
#

you know the shape, do you really need the loop

dense nimbus
#

uhm

#

either loop or 10 if's

high pine
#

the loop still has those 10 ifs

dense nimbus
#

yes but looks nicer in switch

high pine
#

or if the loop helps with the ifs, then you can do just fine with a helper function

high pine
dense nimbus
#

now im bit confused

high pine
#
const obj = {
  a: 0,
  b: "1"
};

for (const [k, v] of Object.entries(obj)) {
  switch (k) {
    case "a":
      console.log(v + 1);
      break;
    case "b":
      console.log(parseInt(v) + 1);
      break;
  }
}

// or

console.log(obj.a + 1);
console.log(parseInt(obj.b) + 1);
high pine
dense nimbus
#

but that wont work

#

because if i want to assign or do some actions such as regex checking, it will say i cant do that on object

dense nimbus
high pine
#

...why do you want to use a regex on an object?

#

how would using a for..of/switch solve that

dense nimbus
#

i dont... facepalm

high pine
#

im so confused

dense nimbus
#

understandable

#

so

dense nimbus
high pine
#

yeah

dense nimbus
#

so now i wanna do a switch which would fix that and if i have a case 'button', in that case, value will be treated as string

#

same as for if i do case 'uptimeLogs', value inside will be treated as that object

high pine
#

why can't you just do obj.button and know it's a string

#

without the loop+switch

dense nimbus
#

i can but then i would has 10 if's as said earlier

high pine
#

how?

dense nimbus
#

if (obj.button) {}
if (obj.message_id) {}
if (obj.role_mention) ...

high pine
#

no wtf

#

what?

dense nimbus
#

what

high pine
#

no just do obj.button

dense nimbus
#

but i need for multiple properties lol

high pine
#

what would those ifs be doing?

high pine
#

you don't need ifs for that

dense nimbus
#

what

high pine
#
for (const [key, value] of ...) {
  switch (key) {
    case "button":
      someFunctionUsingString(value);
      break;
    ...
  }
}
```suppose this as a placeholder for your proposed loop, does this make sense
dense nimbus
high pine
#

module.button = body.button ?? null;???

dense nimbus
#

and what if body.button is random string

high pine
#

it shouldn't be to begin with?

#

are these inputs unsafe

dense nimbus
#

yes

high pine
#

i see the issue

dense nimbus
#

rest route for updating

high pine
#
module.button = body.button?.match(...) ? body.button : null
#

why not this then

dense nimbus
#

because

#

uh

#

well

#

isnt that just same as if

high pine
#

you'd need some form of if on the condition here anyways

dense nimbus
#

in more compact way

high pine
#

yes

#

using a loop and switch would just be obfuscation, considering you need ifs afterwards

dense nimbus
#

yeah

#

well good to know

#

thanks tho lol

high pine
#

also Object.entries isn't typesafe but that wouldn't have mattered in the context given, it'd just be annoying to convince ts

dense nimbus
#

i noticed that

high pine
#

i don't think the initial check is required there

#

that's what ?. does

dense nimbus
#

yeah i know, but does it really change anything

high pine
#

makes it less readable i'd argue

#

optional chaining or separate check

#

having both is confusing

#

to me, at least

dense nimbus
#

well ill make it with ?. but true

dense nimbus
#

that way i can have safe input

high pine
#

idk networking, no clue if that's safe

dense nimbus
#

rip

high pine
#

wait isn't this just cors

#

am i tripping

dense nimbus
#

what

high pine
#

or is that on the other side

dense nimbus
#

wait