Questions tagged [union-types]

This is a tag to discuss the Union Type feature as seen in languages such as TypeScript, Ceylon or F#.

277 questions
96
votes
5 answers

What does "keyof typeof" mean in TypeScript?

Example: Explain to me what keyof typeof means in TypeScript enum ColorsEnum { white = '#ffffff', black = '#000000', } type Colors = keyof typeof ColorsEnum; The last row is equivalent to: type Colors = "white" | "black" But how does it…
user1283776
  • 12,822
  • 32
  • 103
  • 190
61
votes
3 answers

mypy, type hint: Union[float, int] -> is there a Number type?

mypy is really handy and catches a lot of bugs, but when I write "scientific" applications, I often end up doing: def my_func(number: Union[float, int]): # Do something number is either a float or int, depending on the user's input. Is there an…
JPFrancoia
  • 3,490
  • 6
  • 30
  • 56
40
votes
2 answers

How to define string literal union type from constants in Typescript

I know I can define string union types to restrict variables to one of the possible string values: type MyType = 'first' | 'second' let myVar:MyType = 'first' I need to construct a type like that from constant strings, e.g: const MY_CONSTANT =…
Can Poyrazoğlu
  • 29,145
  • 40
  • 152
  • 327
17
votes
1 answer

What's the difference between a constrained TypeVar and a Union?

If I want to have a type that can be multiple possible types, Unions seem to be how I represent that: U = Union[int, str] U can be an int or a str. I noticed though that TypeVars allow for optional var-arg arguments that also seem to do the same…
Carcigenicate
  • 35,934
  • 8
  • 48
  • 81
15
votes
2 answers

PHP Type hinting to allow Array or ArrayAccess

Is it possible to allow an Array or an object that implements ArrayAccess? For example: class Config implements ArrayAccess { ... } class I_Use_A_Config { public function __construct(Array $test) ... } I want to be able to pass in…
Supericy
  • 5,735
  • 1
  • 18
  • 24
12
votes
1 answer

How to extract a type from a tagged union type in typescript?

Say there is already a type defined as this: export type Item = { type: 'text', content: string } | { type: 'link', url: string } Is it possible to extract the link part from the type Item? I mean, is it possible to define a type…
Freewind
  • 177,284
  • 143
  • 381
  • 649
10
votes
2 answers

How to type a Typescript array to accept only a specific set of values?

I am writing a type declaration file for a library I do not control. One of the methods accepts an array of strings as a parameter, but these strings can only be very specific values. Currently I am typing this parameter as a string[], but I was…
Shaun Hamman
  • 1,947
  • 3
  • 16
  • 33
9
votes
4 answers

Algebraic Data Types in Postgres

Is it possible to create an Algebraic Data Type in Postgres and then use it as a column type? For example: CREATE TYPE hoofed AS ENUM('horse', 'goat'); CREATE TYPE monkey AS ENUM('chimp','macaque'); CREATE TYPE ANIMAL AS ENUM(hoofed,…
Abraham P
  • 13,493
  • 11
  • 50
  • 108
8
votes
4 answers

Mixing union types, generics and conditional types causes unexpected "Type is not assignable to type" error

I've hit a problem with type-inference specifically when conditional-types are used within union types. There may be a shorter way to demonstrate this issue, but I could not find one... See the problem in action at this playground link. Consider the…
spender
  • 106,080
  • 28
  • 202
  • 324
8
votes
1 answer

Elm Tagged Union Type compare constructor

Is it possible to use == to compare the constructor of tagged union types in Elm, or do you have to use case? Example: type alias Model = { accessToken : String , page : Page , config : Config } type Page = Home String …
Xyking
  • 83
  • 4
7
votes
1 answer

How does Dotty decide how to infer/when to widen Union Types?

Widening Union Types have been discussed here but I can't seem to find an answer to the following case Let's start by looking at the following val x = List(1, 2, "a") This heterogeneous list is inferred as List[Any] Just like it would in Scala…
sinanspd
  • 1,989
  • 2
  • 14
  • 28
7
votes
2 answers

"Unsupported target for indexed assignment" with mypy, depending on type hinting moment with respect to assignment

I'm trying to do some typing on my python code, and I got the following mypy error: "Unsupported target for indexed assignment" On a simplified example, it amounts to the following code: from pathlib import Path from typing import (Literal,…
bli
  • 5,749
  • 2
  • 34
  • 76
7
votes
1 answer

TS2339: Property does not exist on union type - property string | undefined

I have problem with my union type, which looks like this: type RepeatForm = { step: | { repeat: false; } | { repeat: true; from: undefined; } | { …
Konrad Klimczak
  • 1,047
  • 2
  • 16
  • 38
6
votes
0 answers

Generic union type inferred differently from equivalent direct union type

Consider the following: type UnwrapNullable1 = Required<{ x?: (1 | undefined) }>["x"] type UnwrapNullable = Required<{ x?: T }>["x"] type Test = UnwrapNullable<1 | undefined> The type UnwrapNullable1 is correctly inferred as 1, but Test, which…
prmph
  • 4,827
  • 7
  • 31
  • 35
6
votes
3 answers

Typescript - how to combine Union and Intersection types

I have the following component: export enum Tags { button = 'button', a = 'a', input = 'input', } type ButtonProps = { tag: Tags.button; } & ({ a?: string; b?: undefined } | { a?: undefined; b?: string }) & …
leepowell
  • 3,404
  • 7
  • 23
  • 33
1
2 3
18 19