随笔分类 - TypeScript
摘要:To fix the CommonJS export issue, we need to make a change to our tsconfig.json file. Add the verbatimModuleSyntax setting and set it to true: { "comp
阅读全文
摘要:The module and moduleResolution options in the tsconfig.json file can be confusing. Here we'll discuss the differences between the module and moduleRe
阅读全文
摘要:Here we're attempting to import several PNG files into our TypeScript program: import pngUrl1 from "./example1.png"; // red squiggly line under "./exa
阅读全文
摘要:Here we're importing a function myModuleFunc from my-module: import { myModuleFunc } from "my-module"; // red squiggly line under "my-module" Let's st
阅读全文
摘要:In tsconfig file, you have targetand libsconfiguration. You always need to define a target, recommended as es2022 Specifying the lib option also lets
阅读全文
摘要:The declare keyword in TypeScript allows you to specify types for global variables. Whenever you use it, an ambient context is created, which means th
阅读全文
摘要:Typescript check a file whether it contains any export/import, if it is, then it's a module; if not then it's a script. What's the difference between
阅读全文
摘要:/** * How do we annotate the errors this function throws? */ type PossibleErrors = SyntaxError | DOMException; const getUserFromLocalStorage = (id: st
阅读全文
摘要:For the following code: const objOfFunctions = { string: (input: string) => input.toUpperCase(), number: (input: number) => input.toFixed(2), boolean:
阅读全文
摘要:import { expect, it, vitest } from 'vitest'; const logId = (obj: { id: string }) => { console.log(obj.id); }; const logName = (obj: { name: string })
阅读全文
摘要:Make those pass: import { Equal, Expect } from "@total-typescript/helpers"; type Event = "click" | "hover" | "scroll"; type CallbackType = unknown; co
阅读全文
摘要:function pipe<A, B>(fn: (a: A) => B) { function run(a: A) { return fn(a) } run.pipe = <C, >(fn2: (b: B) => C) => pipe((a: A) => fn2(fn(a))) return run
阅读全文
摘要:type BaseTable = { [colName: string]: string | number | boolean; } type Columns<Tables extends { [tableName: string]: BaseTable }> = { [K in keyof Tab
阅读全文
摘要:class QueryBuilder { private fields: string[] = [] private wheres: Record<string, string> = {} private table: string = "" select(...columns: string[])
阅读全文
摘要:import { expect, it, vitest } from 'vitest'; interface User { id: number; name: string; } function printUser(user: User) { Object.keys(user).forEach((
阅读全文
摘要:interface User { id: number; name: string; } const users = [ { name: 'Waqas', }, { name: 'Zain', }, ]; const usersWithIds: User[] = users.map((user, i
阅读全文
摘要:The error we encountered in this challenge was that the EventTarget | null type was incompatible with the required parameter of type HTMLFormElement.
阅读全文
摘要:Value Objects are another pattern in Domain-driven Design that provide more structure around what you can and cannot do with a type. In TypeScript we
阅读全文
摘要:TypeScript will sometimes display the original Primitive Type rather than the Type Alias that you've set. By appending & {} to your Type Alias, you ca
阅读全文
摘要:We start with three interface types: User, Organisation, and Product. These types share common properties: id, name, and imageId, but each also posses
阅读全文