I have 2 objects objA and objB
I have a function which accepts value from objB and key from objA.
const objA = {
a:"a",
b:"b"
}
const objB = {
a:"a",
b:"b"
}
checkValues = (type:string,key:keyof typeof objA)=>{
return objA[key] === type;
}
checkValues("A",objA.a)
But getting type error
Argument of type 'string' is not assignable to parameter of type '"a" | "b"'
Defining your function like that it can only check the entries of the object(so 'a' | 'b'), instead, you calling the function passing objA.a which instead refers to the value of the property a on the object.
What you can do is:
//Calling the function and passing only 'a' or 'b'
checkValues("A", 'a');
checkValues("A", 'b');
//Declaring object properties with types through the use of interfaces:
interface MyObj {
a: 'a' | 'b';
b: 'a' | 'b';
}
const objA: MyObj = {
a:"a",
b:"b"
}
const objB: MyObj = {
a:"a",
b:"b"
}
You haven't really specified what you need so this is what i would probably do, hope it will help