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

Constraining type in Typescript generic to be one of several types

Updated for TS3.5+ on 2019-06-20 Issue #1: K extends string | number for the index signature parameter: Yeah, this can’t be done in a very satisfying way. There are a few issues. The first is that TypeScript only recognizes two direct index signature types: [k: string], and [k: number]. That’s it. You can’t do (edit … Read more

Discriminated union in C#

I don’t really like the type-checking and type-casting solutions provided above, so here’s 100% type-safe union which will throw compilation errors if you attempt to use the wrong datatype: using System; namespace Juliet { class Program { static void Main(string[] args) { Union3<int, char, string>[] unions = new Union3<int,char,string>[] { new Union3<int, char, string>.Case1(5), new … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)