Narrowing a return type from a generic, discriminated union in TypeScript

Like many good solutions in programming, you achieve this by adding a layer of indirection. Specifically, what we can do here is add a table between action tags (i.e. “Example” and “Another”) and their respective payloads. type ActionPayloadTable = { “Example”: { example: true }, “Another”: { another: true }, } then what we can … Read more

Global types in typescript

Yes this is possible. You can find all information here: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html The important part is this: declare global { /*~ Here, declare things that go in the global namespace, or augment *~ existing declarations in the global namespace */ interface String { fancyFormat(opts: StringFormatOptions): string; } }

Types from both keys and values of object in Typescript

The compiler will widen string literal type to string, unless some specific conditions are met as explained in github issues and PR, or const assertion is used for literal value. Const assertions appeared in TypeScript 3.4: const KeyToVal = { MyKey1: ‘myValue1’, MyKey2: ‘myValue2’, } as const; type Keys = keyof typeof KeyToVal; type Values … Read more

Extend (Update) third-party old type declaration interface with new one

If you need to overwrite an existing property declaration to change the type, you’ll need to fork the @types/expo types. The easiest way is probably to copy the index.d.ts file into your typings directory and uninstall the original @types/expo package. Or you can use a tool such as Braid (disclosure: I am a Braid contributor) … Read more

How should I use @types with TypeScript 2

It’s very simple. Just install the definitions that you need via npm. For example if you need lodash you can do: npm install –save @types/lodash Once it’s installed you can use it right away in your project. Typescript will resolve the typings for the installed @types package from the node_modules/@types folder by default. There’s no … Read more

ts-node ignores d.ts files while tsc successfully compiles the project

I was having a similar problem, but I could not add –files, because I run ts-node by registering the module through mocha (i.e. mocha -r ts-node/register …). I could solve it by adding a files and a ts-node section to tsconfig.json like this: // tsconfig.json { “ts-node”: { “files”: true }, “files”: [ “src/index.ts”, “src/global.d.ts” … Read more

TypeScript typings in npm @types org packages

As of TypeScript 2.0, typings is no longer required. The npm organization is an entity to setup a developers team. I believe Microsoft setup the @types organization in npm and added the TypeScript developer team to the organization. Packages on under the @types organization are published automatically from DefinitelyTyped using the types-publisher tool as per … Read more

Mapping tuple-typed value to different tuple-typed value without casts

No, this is not possible in TypeScript as of TS4.1. There are two issues I see; one might be overcome with a change to the TypeScript standard library typings for Array.prototype.map(), but the other would require a fairly large change to the type system to work in general and cannot currently be handled in a … Read more

Exclude/overwrite npm-provided typings

Create node_modules folder under your src, then put typings of module(s) you want to overwrite inside: ├── node_modules │ └── … │ └── src ├── index.ts ├── … your codes … │ └── node_modules └── <module-to-be-overwritten> └── index.d.ts No need to modify compilerOptions in tsconfig.json. Read How TypeScript resolves modules section in https://www.typescriptlang.org/docs/handbook/module-resolution.html.

How to configure custom global interfaces (.d.ts files) for TypeScript?

“Magically available interfaces” or global types is highly discouraged and should mostly be left to legacy. Also, you should not be using ambient declaration files (e.g. d.ts files) for code that you are writing. These are meant to stand-in the place of external non-typescript code (essentially filling in the typescript types into js code so … Read more