Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript : Property does not exist on type 'object'

I have the follow setup and when I loop through using for...of and get an error of :

Property "country" doesn't exist on type "object".

Is this a correct way to loop through each object in array and compare the object property value?

let countryProviders: object[];  export function GetAllProviders() {    allProviders = [       { region: "r 1", country: "US", locale: "en-us", company: "co 1" },       { region: "r 2", country: "China", locale: "zh-cn", company: "co 2" },       { region: "r 4", country: "Korea", locale: "ko-kr", company: "co 4" },       { region: "r 5", country: "Japan", locale: "ja-jp", company: "co 5" }    ]     for (let providers of allProviders) {       if (providers.country === "US") { // error here          countryProviders.push(providers);       }    } } 
like image 591
Jason Avatar asked Apr 11 '17 06:04

Jason


People also ask

How do you check if a property exists in an object TypeScript?

To check if a property exists in an object in TypeScript: Mark the specific property as optional in the object's type. Use a type guard to check if the property exists in the object. If accessing the property in the object does not return a value of undefined , it exists in the object.

How do you ignore property does not exist on type?

Use a type assertion to ignore the 'Property does not exist on type' error in TypeScript, e.g. (obj as any). myProperty . Casting the object to any disables type checking and allows us to access any property on the object without getting any errors.

Does not exist on type string TS2339?

To fix the error "TS2339: Property 'x' does not exist on type 'Y'" with TypeScript, we should make sure the properties are listed in the interface that's set as the type of the object. interface Images { main: string; [key: string]: string; } const getMainImageUrl = (images: Images): string => { return images. main; };

Is missing the following properties from type?

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.


2 Answers

You probably have allProviders typed as object[] as well. And property country does not exist on object. If you don't care about typing, you can declare both allProviders and countryProviders as Array<any>:

let countryProviders: Array<any>; let allProviders: Array<any>; 

If you do want static type checking. You can create an interface for the structure and use it:

interface Provider {     region: string,     country: string,     locale: string,     company: string }  let countryProviders: Array<Provider>; let allProviders: Array<Provider>; 
like image 189
Saravana Avatar answered Oct 05 '22 18:10

Saravana


If your object could contain any key/value pairs, you could declare an interface called keyable like :

interface keyable {     [key: string]: any   } 

then use it as follows :

let countryProviders: keyable[]; 

or

let countryProviders: Array<keyable>; 

For your specific case try to define the key type and use Record utility to define the object :

type ProviderKey="region" | "country" | "locale" | "company"  let countryProviders: Array<Record<ProviderKey,string>>;  
like image 30
Boussadjra Brahim Avatar answered Oct 05 '22 18:10

Boussadjra Brahim