Los tipos utilitarios en TypeScript
Los integrados que vale la pena memorizar, y para qué sirve cada uno.
interface Run {
lang: string;
tpm: number;
stars: number;
note?: string;
}
type Summary = Pick<Run, "lang" | "tpm">;
type WithoutNote = Omit<Run, "note">;
type ByLang = Record<string, Summary>;
type Patch = Partial<Run>;
type Complete = Required<Run>;
async function load(): Promise<Run[]> {
return [];
}
type Loaded = Awaited<ReturnType<typeof load>>;
type Args = Parameters<(lang: string, tpm: number) => void>;
const summary: Summary = { lang: "ts", tpm: 98 };
const table: ByLang = { ts: summary };
const patch: Patch = { stars: 3 };
const rows: Loaded = [];
const args: Args = ["ts", 98];
console.log(table.ts?.tpm, patch.stars, rows.length, args[0]);
Cómo funciona
PickyOmitseleccionan o descartan claves.Recordconstruye un tipo de mapa;Awaiteddesenvuelve una promesa.ReturnTypeyParametersleen el tipo de una función.
Palabras clave y builtins usados aquí
ArgsByLangLoadedPatchPromiseSummaryasyncconstfunctioninterfacenumberreturnstringsummarytype
El intento, en números
- Líneas
- 26
- Caracteres a escribir
- 660
- Tokens
- 196
- Ritmo de tres estrellas
- 100 tpm
Al ritmo de tres estrellas de 100 tokens por minuto, este intento toma unos 118 segundos.
Paso 4 de 4 en Leer tipos; paso 8 de 20 en Genéricos.