DEV Community

Ahmad Tibibi
Ahmad Tibibi

Posted on

TS1110: Type expected

TS1110: Type expected

TypeScript is a powerful programming language that builds on JavaScript by adding static type definitions. This means that while JavaScript is a dynamically typed language—where types are determined at runtime—TypeScript enables developers to specify types at compile time. Types in TypeScript can include primitives like number, string, and boolean, as well as complex types like objects, arrays, interfaces, and enums. This static typing concept helps catch errors early in the development process, providing better documentation and tooling support.

If you want to learn more about TypeScript or utilize AI tools like gpteach to enhance your coding skills, be sure to subscribe to my blog!

What Are Enums?

Enums (short for "enumerations") are a special feature in TypeScript that allows developers to define a set of named constants. They are a way to create a collection of related values that can be accessed by name, improving code readability and maintainability. For example:

enum Direction {
    Up,
    Down,
    Left,
    Right
}

let move: Direction = Direction.Up;
console.log(move); // 0
Enter fullscreen mode Exit fullscreen mode

In this example, Direction is an enum with four possible values. Each enumerated value is assigned a numeric value starting from 0 by default.

Understanding TS1110: Type expected.

Now let's dive into a common TypeScript error: TS1110: Type expected. This error usually occurs during type definitions when TypeScript expects you to specify a type but you have omitted it.

Common Scenarios Leading to TS1110: Type expected.

  1. Function Parameters without Types

Here's an example of code that will trigger the TS1110: Type expected. error:

function greet(name) {
    console.log(`Hello, ${name}`);
}
Enter fullscreen mode Exit fullscreen mode

In the above code, the parameter name does not have a type annotation. To fix this, you simply need to specify the type for name:

function greet(name: string) {
    console.log(`Hello, ${name}`);
}
Enter fullscreen mode Exit fullscreen mode

Important to know!

When defining function parameters, always provide type annotations to avoid the TS1110: Type expected. error.

  1. Interface Definitions

An interface definition without types will also trigger this error:

interface User {
    name
    age: number;
}
Enter fullscreen mode Exit fullscreen mode

The missing type for name will cause a TS1110: Type expected. error. To resolve this, add the appropriate type:

interface User {
    name: string;
    age: number;
}
Enter fullscreen mode Exit fullscreen mode

Important to know!

Interfaces are powerful tools in TypeScript to define the structure of an object, and always require types for each property.

  1. Enum Definition Issues

Enums need specific type annotations as well. Here's an example of incorrect code:

enum Colors {
    Red,
    Green,
    Blue
    Yellow
}
Enter fullscreen mode Exit fullscreen mode

This will trigger a TS1110: Type expected. error due to the missing comma after Blue. Correct it by adding the missing comma:

enum Colors {
    Red,
    Green,
    Blue,
    Yellow
}
Enter fullscreen mode Exit fullscreen mode

Important to know!

Always ensure proper syntax in your enum definitions to avoid the TS1110: Type expected. error.

FAQ Section

  • What causes the TS1110: Type expected. error?
    The error is caused by a missing type annotation in various TypeScript constructs like function parameters or interface definitions.

  • How can I avoid getting the TS1110: Type expected. error?
    Always be diligent about providing type annotations throughout your TypeScript code.

  • Is TS1110 a common error for beginners?
    Yes, it's especially common when people first start learning TypeScript and are not used to having to explicitly specify types.

Conclusion

The error TS1110: Type expected. is a common hurdle for developers transitioning to TypeScript from JavaScript. By understanding the importance of type definitions in functions, interfaces, and enums, you can write more robust and error-free code. Always remember to provide type annotations, check for syntax errors, and reference your documentation as needed to avoid such errors.

If you found this information helpful, consider subscribing to my blog for more TypeScript tips and tricks!

Top comments (0)