Type aliases and type assertions

Type aliases

A type alias is used to give a type a new name. It is often used for union types, similar to the interface,

Use the type keyword to create a type alias

type Name = string; // Return a string type NameResolver = () => string; / / receive a function And return a string type NameOrResolver = Name | NameResolver; Function getName(n: NameOrResolver): Name {if (typeof n === 'string') {return n; } else { return n(); }}Copy the code

Disadvantages: You can only define one variable at a time. If you define multiple variables, the best way is to use interface

Type Assertion Type Assertion

Type assertions can be used to manually specify the type of a value.

Type assertion is like conversion in other languages, but without special data checking and deconstruction. It has no run-time impact, only at compile time. Abusing type assertions can lead to runtime errors:

Type assertions come in two forms. The first is the Angle bracket syntax:

let someValue: any = "this is a string"; Let strLength: number = (<string>someValue).length;Copy the code

The other is as syntax: but when you use JSX in TypeScript, only AS syntax assertions are allowed

let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
Copy the code

In addition to representing type assertions, ts may also represent a generic type.

We recommend that when using type assertions, we use syntax like value as type, where we really need to access properties or methods specific to one of the types when we are not sure of the type. For example:

interface Cat {
    name: string;
    run(): void;
}
interface Fish {
    name: string;
    swim(): void;
}

function isFish(animal: Cat | Fish) {
    if (typeof animal.swim === 'function') {
        return true;
    }
    return false;
}

// index.ts:11:23 - error TS2339: Property 'swim' does not exist on type 'Cat | Fish'.
//   Property 'swim' does not exist on type 'Cat'.
Copy the code

In the example above, an error was reported when getting Animal. swim.

Declare animal to Fish with type assertion:

interface Cat { name: string; run(): void; } interface Fish { name: string; swim(): void; } function isFish(animal: Cat | Fish) { if (typeof (animal as Fish).swim === 'function') { return true; } return false; } This can solve the problem of visiting animal.swim Times.Copy the code

(Animal as Fish).swim() = animal as Cat The TypeScript compiler trusts our assertion so there is no compilation error when calling Swim ().

But accept parameters is Cat | Fish swim function, once the incoming parameter is the type of variable, Cat with no swim method, the Cat will result in a runtime error.

In summary, be careful when using type assertions and avoid invoking methods or referencing deep properties after assertions to reduce unnecessary runtime errors.

Type assertion is also widely used in classes. If you are interested, you can go to this part of the content. This article will not go into details.