2021年4月25日星期日

How to restrict string parameter to some keyof alue

I'am working with the Array.some function in TypeScript. I need to restrict the possible strings I will receive as the keys of some Object.

Form example I have an array with some names of the object's properties and my object implements an interface:

    const props = ["a", "b", "c"]        interface A {          a: any,          b: any,          c: any      }  

Then I call some function

    props.some( (prop) => {          // ...      })  

I tried with:

    props.some( (prop: keyof A) => {          // ...      })  

But I received "Type string is not assignable to type keyof A"

How is this usually resolved?

https://stackoverflow.com/questions/67259996/how-to-restrict-string-parameter-to-some-keyof-alue April 26, 2021 at 10:04AM

没有评论:

发表评论