Questions tagged [discriminated-union]

Discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types. They are also known as "sum types" in type theory. For [ocaml] use [variant] instead.

Discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types.

Discriminated unions are most important in functional languages such as and , where the compiler is able to verify that all cases of a disjoint union are always handled, avoiding many types of errors.

For a brief history of discriminated union, please visit its wikipedia page.

Related tags

372 questions
0
votes
1 answer

TypeScript union with required discriminant string

The problem i'm trying to solve is making sure that type of the parameter passed to the doSomething function is a union, which has __typename: 'GenericError' option. This is extract form the functionality i'm working: type TypeNamedResponse = { …
tszarzynski
  • 604
  • 7
  • 15
0
votes
1 answer

How to read a database record into a discriminated union?

Let's say I have the following in F#: type PersonName = { birthDate : DateTime firstName : string lastName : string } Followed by the discriminated union of: type Visit = | Scheduled of name: PersonName * appointmentTime: DateTime |…
Alan Wayne
  • 4,332
  • 9
  • 39
  • 77
0
votes
1 answer

Pydantic: Validate discriminated union with literals

I am trying to use Literal to create a discriminated union with Pydantic. There are events about a Job resource, and I want to distinguish them by event_name. For JobPublishedEvents I want to ensure, that some extra_field is present. class…
elactic
  • 675
  • 5
  • 22
0
votes
1 answer

How do I keep a type as generic in f#?

I'm struggling with setting up an f# function so that that it can accept type (from c#) Dictionary where keys is just type enum and dynamic would be a scruct type which holds parameters. My f# function looks like this: let func…
0
votes
1 answer

Fixed Typescript type to a Union Type

I have the following: type Original = { checklist: FetchStates; workflowFormInfo: FetchStates; transitions: FetchStates; } type FinalResult = {checklist: FetchStates} | { workflowFormInfo: FetchStates} | { transitions:…
Renan Cidale
  • 613
  • 2
  • 7
  • 19
0
votes
0 answers

Can I augment all DU types with a method that can access the type itself, in F#?

I am trying to define an DU where I can query the actual type to perform some operations based on the name. This is used to build string ids where the name of the DU type is used. for example: type MyDU = | TypeA | TypeB member…
Thomas
  • 7,832
  • 5
  • 39
  • 87
0
votes
1 answer

how to enumerate a discriminated union to get both the name and type instance, in F#?

First, I read this: How to enumerate a discriminated union in F#? but it doesn't really solve my issue. I am trying to parse a discriminated union, get all the types, get their names and then put them in a dictionary (well, more than that but it's…
Thomas
  • 7,832
  • 5
  • 39
  • 87
0
votes
2 answers

Can Typescript guard / discriminate based on field presence (undefined / absent) rather than value?

Can anyone explain why Typescript can narrow types with the in keyword, but not by presence an non-undefined value? I'm porting a large codebase from JS to TS, and very extensive use has been made of the if (x.something) { ... } construct. declare…
0
votes
1 answer

Discriminate function and object types based on an enum member

I'm trying to get narrowed types in TypeScript when working with index signatures and union types without discriminating them directly, e.g. using a switch case statement. The code below throws an error when calling the doubleFn variable with the…
srn
  • 131
  • 7
0
votes
1 answer

TypeScript Discriminated Union - Variable Used Before Assigned after Exhaustive If Statements

I am writing some code using TypeScript's discriminated union feature. I am exhaustively checking the "type" property for each type in an if-else if structure and assigning a variable using logic in each block. Then, I am returning the variable.…
dawsonc623
  • 847
  • 1
  • 9
  • 21
0
votes
2 answers

Why does TypeScript throw an error when searching for a string in an array with .includes?

In the following example code, TypeScript throws a Argument of type 'string' is not assignable to parameter of type 'Item'. ts(2345) error. type Item = 'foo' | 'bar' | 'baz' const isInArray = (str: string) => { const arr: Item[] = ['foo', 'bar',…
0
votes
1 answer

How do I make a generic constraint for a superset of a discriminated union

When defining a generic class Foo, where X is intended to be a discriminated union type, is there a way to express "X must be a superset of the discriminated union Y"? I have a situation where I am using a discriminated union to represent…
uglycoyote
  • 1,164
  • 13
  • 23
0
votes
0 answers

TypeScript defining key and value for Object probably with discrimination union

I want to define what kind of value is permitted inside an object, based on a defined interface/type: type StorageValuesSyncOptions = { toDoOrNotToDo: boolean, piecesOfEight: number, poem: string, }; type SyncOptions
0
votes
1 answer

Is recursive array types possible in union discrimination?

I'm just starting discovering TypeScript and I'm testing the limits. What I'm looking for is a way to make an interface that as fields depending on the value of one of it's property. For example: type type RecursiveArray = T |…
GaldanM
  • 85
  • 10
0
votes
1 answer

How to create an instance of a discriminated union in TypeScript based on a provided type?

Given the discriminated union MyUnion, I want to call a function createMyUnionObject with one of the types of MyUnion and a value for value which must be the correct type. type MyUnion = { type: 'one' value: string } | { …
Kiwi
  • 1,023
  • 10
  • 24