Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (2024)

# Table of Contents

  1. Property does not exist on type '{}' in TypeScript
  2. Don't use the Object type when typing objects
  3. Property does not exist on type Union in TypeScript
  4. Property 'status' does not exist on type 'Error' in TS

# Property does not exist on type '{}' in TypeScript

The "Property does not exist on type '{}'" error occurs when we try to accessor set a property that is not contained in the object's type.

To solve the error, type the object properties explicitly or use a type withvariable key names.

Here is an example of how the error occurs:

index.ts

Copied!

const obj = {};// ⛔️ Error: Property 'name' does not exist on type '{}'.ts(2339)obj.name = 'Bobby Hadz';

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (1)

You might also get the error if you use the Object type.

index.ts

Copied!

const obj: Object = {};// ⛔️ Property 'name' does not exist on type 'Object'.obj.name = 'Bobby Hadz';

We didn't explicitly type the obj variable and initialized it to an emptyobject, so we aren't able to assign or access properties that don't exist on theobject's type.

# Explicitly type the object to solve the error

To solve the error,type the object explicitly and specifyall of the properties you intend to access.

index.ts

Copied!

// ✅ When you know property names ahead of timetype Employee = { id?: number; name?: string;};const obj: Employee = {};obj.id = 1;obj.name = 'Bobby Hadz';

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (2)

The code for this article is available on GitHub

If you don't know all of the property names ahead of time, use anindex signature.

index.ts

Copied!

// ✅ When you don't know ALL property names ahead of timetype Employee = { [key: string]: any; // 👈️ variable key name: string;};const obj: Employee = { name: 'Bobby Hadz',};obj.salary = 100;

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (3)

The first example shows how to type an object when you know its property namesand the type of its values ahead of time.

We need todeclare the object as empty, so wemarked the properties asoptionalby using a question mark.

index.ts

Copied!

// ✅ When you know property names ahead of timetype Employee = { id?: number; name?: string;};const obj: Employee = {};obj.id = 1;obj.name = 'Bobby Hadz';

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (4)

Now the object can be initialized as empty and the id and name properties can be assigned later on.

The properties exist on the object's type, so we won't get the "Property doesnot exist on type 'Object'" error when accessing them.

You can alsomake all of an object's properties optionalby using the Partial type.

# When you don't know the names of all of the object's properties

In some cases, you won't know the names of all of the object's keys or the shapeof the values ahead of time.

index.ts

Copied!

type Employee = { [key: string]: any; // 👈️ variable key name: string;};const obj: Employee = { name: 'Bobby Hadz',};obj.salary = 100;

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (5)

The code for this article is available on GitHub

The{[key: string]: any}syntax is called anindex signatureand is used when you don't know the names of the object's keys or the shape ofthe values ahead of time.

The syntax means that when the object is indexed with a string key, it willreturn a value of any type.

If you know any of the names of the properties ahead of time, you can specify them to get better type safety.

We set the name property to string in the Employee type, so the typechecker would throw an error if the property is not provided or is set to avalue of a different type.

# If you don't know the names of all keys but know the shape of the values

If you don't know the names of all of the object's keys but know the shape ofthe values, you can use a more specific index signature for better type safety.

index.ts

Copied!

type Employee = { [key: string]: string | number; name: string; id: number;};const obj: Employee = { id: 1, name: 'Bobby Hadz',};obj.department = 'accounting';obj.salary = 100;

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (6)

The code for this article is available on GitHub

The index signature means that when the object is indexed with a string key, itwill return a value that has a string or number type.

The string | number syntax is called aunion type in TypeScript.

We had to use a union type because the name and id properties are strings and they return different types.

In other words, you can't specify that when the object is indexed with a stringkey, it returns a value of type string and then add another string key to theinterface that has a value oftype number.

index.ts

Copied!

type Employee = { [key: string]: string; name: string; // ⛔️ Error: Property 'id' of type 'number' is // not assignable to 'string' index type 'string'.ts(2411) id: number;};

The example shows that the type checker throws an error if we specify that whenindexed with a string key, the object returns a value of type string and tryto add another string key that has a value of type number.

# Use the any type if you want to turn off type-checking

If you just want to turn off type checking and be able to add any property tothe object, set its type to any.

index.ts

Copied!

const obj: any = {};obj.name = 'Bobby Hadz';obj.age = 30;console.log(obj); // 👉️ { name: 'Bobby Hadz', age: 30 }

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (7)

The code for this article is available on GitHub

The any type effectivelyturns off type checking and enables usto add any property to the object.

However, when we use this approach we don't take advantage of the benefits thatTypeScript provides.

# Don't use the Object type when typing objects

The following code sample raises the same error when we try to access a propertythat doesn't exist on the object's type.

index.ts

Copied!

const obj: Object = { name: 'Bobby Hadz', age: 30,};// ⛔️ Error: Property 'country' does not exist on type 'Object'.ts(2339)obj.country = 'Chile';

We typed the obj variable as Object and tried to access the countryproperty on the object.

However, the country property does not exist on the Object type, so the type checker throws an error.

This is only 1 of the issues in the code sample.

The Object type should never be used to type a value in TypeScript.

The Object type means "any non-nullish value". In other words, it means anyvalue that is not null or undefined.

# Property does not exist on type Union in TypeScript

The "property does not exist on type union" error occurs when we try to accessa property that is not present on every object in the union type.

To solve the error, use a type guard to ensure the property exists on theobject before accessing it.

Here is an example of how the error occurs.

index.ts

Copied!

type Person = { age: number;};type Employee = { salary: number;};function getProperty(obj: Person | Employee): number { // ⛔️ Error: Property 'age' does not exist on type 'Person | Employee'. if (obj.age) { return obj.age; } return obj.salary;}

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (8)

The age property doesn't exist on all of the types in theunion,so we aren't able to access it directly.

# Use a type guard to solve the error

Instead, we have to use a type guardto check if the property exists in the object before accessing it.

index.ts

Copied!

type Person = { age: number;};type Employee = { salary: number;};function getProperty(obj: Person | Employee): number { if ('age' in obj) { return obj.age; } return obj.salary;}

The code for this article is available on GitHub

We used thein operator,which is a type guard in TypeScript.

The in operator returns true if the specified property is in the object orits prototype chain.

index.ts

Copied!

const obj = { a: 'hello',};console.log('a' in obj); // 👉️ trueconsole.log('b' in obj); // 👉️ false

This way, TypeScript can infer the type of the object in the if block andafter the if block.

index.ts

Copied!

type Person = { age: number;};type Employee = { salary: number;};function getProperty(obj: Person | Employee): number { if ('age' in obj) { // 👇️ now `obj` is type `Person` return obj.age; } // 👇️ now `obj` is type `Employee` return obj.salary;}

If the age property is contained in the passed-in object, we can safely use the age property and TypeScript is able to infer the type of obj to be Person.

If the if block doesn't run, then the age property isn't in the passed-inobject and TypeScript infers the type of obj to be Employee.

If you need to check if a string is in a union type, click on thefollowing article.

# Property 'status' does not exist on type 'Error' in TS

The error "Property 'status' does not exist on type 'Error'" occurs becausethe status property is not available on the Error interface.

To solve the error, add the specific property to the Error interface orcreate a custom class that extends from Error.

Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (9)

Here is an example of how the error occurs.

index.ts

Copied!

const err = new Error('Something went wrong');// ⛔️ Property 'status' does not exist on type 'Error'.ts(2339)err.status = 500;// ⛔️ Property 'code' does not exist on type 'Error'.ts(2339)console.log(err.code);

The reason we got the errors in the example above is that the status andcode properties don't exist on the Error interface.

By default, the Error interface has the following properties:

index.ts

Copied!

interface Error { name: string; message: string; stack?: string;}

To get around this, we can extend the Error class.

index.ts

Copied!

export class CustomError extends Error { status: number = 200; code: number = 200; constructor(message: string) { super(message); // 👇️ because we are extending a built-in class Object.setPrototypeOf(this, CustomError.prototype); }}const err = new CustomError('Something went wrong');err.status = 500;console.log(err.status);err.code = 500;console.log(err.code);

The code for this article is available on GitHub

The CustomError class extends from the built-in Error class and adds thestatus and code properties.

If you want to read more on how to extend from the Error class in TypeScript,check out this article.

# Additional Resources

You can learn more about the related topics by checking out the followingtutorials:

  • Create a custom Class that extends from Error in TypeScript
  • Property 'X' does not exist on type Request in TypeScript
  • Property does not exist on type Window in TypeScript
  • Property does not exist on type String in TypeScript [Fixed]
  • Property 'flatMap', 'flat' does not exist on type in TS
Property does not exist on type '{}' in TypeScript [Solved] | bobbyhadz (2024)

FAQs

How do I fix property does not exist on type in TypeScript? ›

Solution 2: Widen the type

You might be more sure than TypeScript that the property exists. In that case, you can add it as an optional property to the object you're manipulating: const example : { doesntExist ?: number; } = {}; example . doesntExist = 1; example .

How to fix property does not exist on type never? ›

To fix the 'does not exist on type never' error, you need to ensure that you are not trying to access properties or call methods on values with the 'never' type.

How to access property of type in TypeScript? ›

Indexed Access Types provide a way to access the properties of an object using an index signature. In TypeScript, an index signature is a way to define the shape of an object with keys that are not known at compile time. ObjectType is the object that we want to access the properties of.

Why is my type never TypeScript? ›

The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns. Variables also acquire the type never when narrowed by any type guards that can never be true.

How to check if property is of type TypeScript? ›

In Typescript, we have three ways to work with it using:
  1. typeof: the keyword helps to check values types, like boolean, string, number, etc.
  2. instanceof: the keyword to compare the object instance with a class constructor.
  3. type guards: The powerful way to check types using typescript feature language.
Aug 10, 2022

How do you declare a property in TypeScript? ›

Parameter properties are declared by prefixing a constructor parameter with an accessibility modifier or readonly , or both. Using private for a parameter property declares and initializes a private member; likewise, the same is done for public , protected , and readonly .

How to check if object property is not undefined in TypeScript? ›

Using the “!” Operator

The “!” operator is called the non-null assertion operator. It tells TypeScript that a variable or property is not null or undefined, even if TypeScript's type checker thinks it could be.

How to ignore TS? ›

The // @ts-ignore comment is used in TypeScript to suppress type checking errors on the next line of code. It's often used when you're certain the code is correct but TypeScript's static type checking disagrees. In the above example, myVariable is declared as a string, but then assigned a number.

How do I fix type errors? ›

Fixing a TypeError

You typically fix a TypeError by casting your value(s) to the correct type. In general, to fix a TypeError, think carefully about the types of the values in the line that Python highlighted for you. The types are not compatible.

How do you make a type property optional in TypeScript? ›

In TypeScript, you can define optional properties in an interface, in a class, in an object, or in a type by using the '? ' modifier after the property name. Optional properties allow to specify that a property may or may not be present on an object of that type.

How to get the property of an object by key in TypeScript? ›

In TypeScript, you can use the Object. keys() method to get an array of the object's own enumerable property names. Then, you can iterate over this array to find and access the value associated with a specific key.

How to get type of type in TypeScript? ›

Approach 1: Using typeof Operator

The typeof operator is a straightforward JavaScript feature that TypeScript also utilizes. It returns a string indicating the data type of its operand.

How to declare type variable in TypeScript? ›

In TypeScript, you declare variables with explicit types by specifying the type of the variable after its name using a colon ( : ) followed by the desired data type which can be string, number, etc. Example 1: Here, the variable age is explicitly declared with the type number .

What to avoid in TypeScript? ›

10 Common Mistakes in Typescript Development
  • Overuse of type “any”
  • Misusing “as” Casts.
  • Ignoring Type-check Errors and Warning.
  • Not Using Strict Mode.
  • Implicit “any” in Function Return Types.
  • Not using “const” and “read-only” correctly.
  • Inconsistent Coding Conventions.
  • Overcomplicating Types.

Why is TypeScript so hard to use? ›

The primary challenge in learning TypeScript lies in its departure from the dynamic nature of JavaScript. Programmers accustomed to JavaScript's flexible type system may find the strict type requirements of TypeScript initially overwhelming.

Which property code does not exist on type error TS 2339? ›

"Property 'code' does not exist on type 'Error'. ts(2339)," it suggests that TypeScript doesn't recognize code as a property of the standard Error object. To handle this, i was extend the Error type with a custom interface that includes the code property.

How do I exclude a property from a type in TypeScript? ›

The Omit utility type is used to create a new type by excluding the specified property from the original type. Example: Here, 'type ExcludedType = Omit<FullType, 'prop2′>' ; : This defines a type ExcludedType by omitting 'prop2' from FullType.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5735

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.