Typescript is wierd
Tags: typescript
Published: 2023-03-31 21:45:30

TypeScript is a popular programming language that adds static type checking to JavaScript. While TypeScript has many benefits, there are also some "weird" parts of the language that can trip up developers who are new to it. In this article, we'll explore some of these weird parts and provide tips for how to work around them.

**_

## The unknown Type
_**
TypeScript has a special type called unknown that represents a value whose type is unknown at compile time. This type is useful when you're working with values that come from dynamic sources, such as user input or external APIs.

However, working with the unknown type can be tricky, as you need to narrow the type down to a more specific type before you can use it. One way to do this is to use type guards, such as the typeof and instanceof operators.

For example, let's say you have a function that takes a value of type unknown and returns a string representation of the value:

`function toString(value: unknown): string {
  // Error: Object is of type 'unknown'.
  return value.toString();
}`
In this code, TypeScript throws an error because we're trying to call the toString() method on a value of type unknown. To fix this error, we can use the typeof operator to check the type of the value:

`function toString(value: unknown): string {
  if (typeof value === 'string') {
    return value;
  } else {
    throw new Error('Value is not a string.');
  }
}`
In this updated code, we're using a type guard to check if the value is a string before calling the toString() method. If the value is not a string, we throw an error.

**_

## Function Overloading
_**
TypeScript supports function overloading, which allows you to define multiple function signatures for a single function. This is useful when you want to provide different ways to call a function based on the arguments passed in.

However, function overloading can be confusing, as TypeScript needs to infer the correct function signature based on the arguments passed in. It's important to ensure that the function signatures are distinct and unambiguous.

For example, let's say you have a function that takes two numbers and returns their sum. However, you also want to allow the function to take two strings and concatenate them:

`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any): any {
  return x + y;
}`
In this code, we're defining two function signatures for the add() function: one that takes two numbers and returns a number, and one that takes two strings and returns a string. Note that we're using the any type for the implementation signature, as we need to accept any type of arguments.

While function overloading can be useful, it can also make the code more difficult to read and understand. It's important to use function overloading judiciously and ensure that the function signatures are clear and unambiguous.

_**

## The Never Type
**_
TypeScript has another special type called never that represents a value that can never occur. This type is useful in situations where you need to throw an error or exit the program, as it indicates that the function will never return normally.

For example, let's say you have a function that takes a number and returns its square root. However, if the number is negative, you want to throw an error:

`function sqrt(x: number): number {
  if (x < 0) {
    throw new Error('Invalid argument.');
  }
  return Math.sqrt(x);
}`
In this code, we're throwing an error if the number is negative. However, TypeScript still infers that the function returns a number. To fix this, we can use the never type to indicate that the function will never return normally:

`function sqrt(x: number): number | never {
  if (x < 0) {
    throw new Error('Invalid argument.');
  }
  return Math.sqrt(x);
}`
In this updated code, we're using the union type number | never to indicate that the function can return either a number or never return at all. This ensures that TypeScript correctly infers the type of the function.

Conclusion

TypeScript is a powerful language that adds static type checking to JavaScript. While TypeScript has some "weird" parts, such as the unknown, function overloading, and never types, these features can be used to write robust and maintainable code. With a little practice, you can become proficient in using these features and take advantage of the full power of TypeScript.