keyof typeof cannot find name

436 Views Asked by At

I attempted to provide a type definition that defines an object as any, because as of now it's fairly open-ended as to what it could be. As well, it has a key for a property within this object.

type Props = {
    obj: any;
    objKey: keyof typeof obj;
};

The result is an error on the line for objKey stating:
Cannot find name 'obj'

If I use this inline with the function arguments, it works without issue as seen below.

({ obj, objKey }: { obj: any; objKey: keyof typeof obj })

Why would this error/warning by produced in the lone type definition, and not the inline argument typing?

1

There are 1 best solutions below

0
On

You should use a generic type parameter:

type Props<P> = {
    obj: P;
    objKey: keyof P;
}