Saltearse al contenido

Manipulación de tipos

Creación de tipos a partir de tipos

Es posible crear tipos nuevos componiendo, manipulando o transformando tipos existentes.

Tipos de intersección (&):

Permiten combinar varios tipos en uno solo:

type A = { foo: number };
type B = { bar: string };
type C = A & B; // Intersection of A and B
const obj: C = { foo: 42, bar: 'hello' };

Tipos de unión (|):

Permiten definir un tipo que puede ser uno de varios:

type Result = string | number;
const value1: Result = 'hello';
const value2: Result = 42;

Tipos mapeados:

Permiten transformar las propiedades de un tipo existente para crear otro nuevo:

type Mutable<T> = {
readonly [P in keyof T]: T[P];
};
type Person = {
name: string;
age: number;
};
type ImmutablePerson = Mutable<Person>; // Properties become read-only

Tipos condicionales:

Permiten crear tipos basados en determinadas condiciones:

type ExtractParam<T> = T extends (param: infer P) => any ? P : never;
type MyFunction = (name: string) => number;
type ParamType = ExtractParam<MyFunction>; // string

Tipos de acceso indexado

En TypeScript es posible acceder y manipular los tipos de las propiedades de otro tipo mediante un índice, Type[Key].

type Person = {
name: string;
age: number;
};
type AgeType = Person['age']; // number
type MyTuple = [string, number, boolean];
type MyType = MyTuple[2]; // boolean

Tipos de utilidad

Pueden utilizarse varios tipos de utilidad integrados para manipular tipos. A continuación se muestran los más habituales:

Awaited<T>

Construye un tipo que desenvuelve recursivamente tipos Promise.

type A = Awaited<Promise<string>>; // string

Partial<T>

Construye un tipo con todas las propiedades de T como opcionales.

type Person = {
name: string;
age: number;
};
type A = Partial<Person>; // { name?: string | undefined; age?: number | undefined; }

Required<T>

Construye un tipo con todas las propiedades de T como obligatorias.

type Person = {
name?: string;
age?: number;
};
type A = Required<Person>; // { name: string; age: number; }

Readonly<T>

Construye un tipo con todas las propiedades de T como de solo lectura.

type Person = {
name: string;
age: number;
};
type A = Readonly<Person>;
const a: A = { name: 'Simon', age: 17 };
a.name = 'John'; // Invalid

Record<K, T>

Construye un tipo con un conjunto de propiedades K de tipo T.

type Product = {
name: string;
price: number;
};
const products: Record<string, Product> = {
apple: { name: 'Apple', price: 0.5 },
banana: { name: 'Banana', price: 0.25 },
};
console.log(products.apple); // { name: 'Apple', price: 0.5 }

Pick<T, K>

Construye un tipo seleccionando las propiedades K especificadas de T.

type Product = {
name: string;
price: number;
};
type Price = Pick<Product, 'price'>; // { price: number; }

Omit<T, K>

Construye un tipo omitiendo las propiedades K especificadas de T.

type Product = {
name: string;
price: number;
};
type Name = Omit<Product, 'price'>; // { name: string; }

Exclude<T, U>

Construye un tipo excluyendo de T todos los valores de tipo U.

type Union = 'a' | 'b' | 'c';
type MyType = Exclude<Union, 'a' | 'c'>; // b

Extract<T, U>

Construye un tipo extrayendo de T todos los valores de tipo U.

type Union = 'a' | 'b' | 'c';
type MyType = Extract<Union, 'a' | 'c'>; // a | c

NonNullable<T>

Construye un tipo excluyendo null y undefined de T.

type Union = 'a' | null | undefined | 'b';
type MyType = NonNullable<Union>; // 'a' | 'b'

Parameters<T>

Extrae los tipos de los parámetros de una función de tipo T.

type Func = (a: string, b: number) => void;
type MyType = Parameters<Func>; // [a: string, b: number]

ConstructorParameters<T>

Extrae los tipos de los parámetros de una función constructora de tipo T.

class Person {
constructor(
public name: string,
public age: number
) {}
}
type PersonConstructorParams = ConstructorParameters<typeof Person>; // [name: string, age: number]
const params: PersonConstructorParams = ['John', 30];
const person = new Person(...params);
console.log(person); // Person { name: 'John', age: 30 }

ReturnType<T>

Extrae el tipo de retorno de una función de tipo T.

type Func = (name: string) => number;
type MyType = ReturnType<Func>; // number

InstanceType<T>

Extrae el tipo de instancia de una clase de tipo T.

class Person {
name: string;
constructor(name: string) {
this.name = name;
}
sayHello() {
console.log(`Hello, my name is ${this.name}!`);
}
}
type PersonInstance = InstanceType<typeof Person>;
const person: PersonInstance = new Person('John');
person.sayHello(); // Hello, my name is John!

ThisParameterType<T>

Extrae el tipo del parámetro ‘this’ de una función de tipo T.

interface Person {
name: string;
greet(this: Person): void;
}
type PersonThisType = ThisParameterType<Person['greet']>; // Person

OmitThisParameter<T>

Elimina el parámetro ‘this’ de una función de tipo T.

function capitalize(this: String) {
return this[0].toUpperCase() + this.substring(1).toLowerCase();
}
type CapitalizeType = OmitThisParameter<typeof capitalize>; // () => string

ThisType<T>

Sirve como marcador de un tipo this contextual.

type Logger = {
log: (error: string) => void;
};
let helperFunctions: { [name: string]: Function } & ThisType<Logger> = {
hello: function () {
this.log('some error'); // Valid as "log" is a part of "this".
this.update(); // Invalid
},
};

Uppercase<T>

Convierte a mayúsculas el nombre del tipo T de entrada.

type MyType = Uppercase<'abc'>; // "ABC"

Lowercase<T>

Convierte a minúsculas el nombre del tipo T de entrada.

type MyType = Lowercase<'ABC'>; // "abc"

Capitalize<T>

Pone en mayúscula la inicial del nombre del tipo T de entrada.

type MyType = Capitalize<'abc'>; // "Abc"

Uncapitalize<T>

Pone en minúscula la inicial del nombre del tipo T de entrada.

type MyType = Uncapitalize<'Abc'>; // "abc"

NoInfer<T>

NoInfer es un tipo de utilidad diseñado para bloquear la inferencia automática de tipos dentro del ámbito de una función genérica.

Ejemplo:

// Automatic inference of types within the scope of a generic function.
function fn<T extends string>(x: T[], y: T) {
return x.concat(y);
}
const r = fn(['a', 'b'], 'c'); // Type here is ("a" | "b" | "c")[]

Con NoInfer:

// Example function that uses NoInfer to prevent type inference
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
return x.concat(y);
}
const r2 = fn2(['a', 'b'], 'c'); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.