Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find name 'keyof'

i'm trying to use the keyof keyword in typescript but it seems to be not found. I'm using typescript 3.5.2 with angular 7.2.2 Thanks in advance.

export class Functions {

  static ListObjectProperties(): string[] {
        let array = keyof Foo;
        return array;
  }
}

I get "Cannot find name 'keyof'"

like image 998
rdethurens Avatar asked Jul 19 '19 10:07

rdethurens


People also ask

What is Keyof in TypeScript?

keyof is a keyword in TypeScript which is used to extract the key type from an object type.

What is extends Keyof?

The extends keyword is used to apply constraints to K, so that K is one of the string literal types only. extends means “is assignable” instead of “inherits”' K extends keyof T means that any value of type K can be assigned to the string literal union types.


1 Answers

You can only use keyof in a type context. For instance:

interface Foo { aProperty: unknown }

const x: keyof Foo = "aProperty"
like image 160
Conaclos Avatar answered Oct 12 '22 02:10

Conaclos