1

I have object properties being looped through generically, some of the number props needed additional calculations, and I ended up with this (simplified) situation. Any help would be greatly appreciated: I get an "unassignable" compile error on both lines in the main function.

interface Data {
  a: number,
  b: number,
  c: number
}
const data = {
  "a": 1,
  "b": 2,
  "c": 3
}

const ar: (keyof Data)[] = ["a", "b", "c"]

type Allowed = Omit<Data, "c">
var allowedAr: (keyof Allowed)[] = [ "a", "b" ]


function main(key: keyof Data) {
  if (allowedAr.includes(key))
    allowedFn(key)
}

function allowedFn(key: keyof Allowed) {}
3
  • 1
    Your question is quite similar to this other one. Commented May 13, 2021 at 2:43
  • 1
    See the answer to this question; you will probably want to use a user-defined type guard function like this code shows. Commented May 13, 2021 at 2:51
  • These are perfect thank you! I didn't realize Arary.includes didn't work as a type guard, and that I could define my own-- the code is really helpful for understanding it too, thanks Commented May 13, 2021 at 2:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.