Skip to content

Latest commit

 

History

History
1314 lines (901 loc) · 15.2 KB

type-comparison.md

File metadata and controls

1314 lines (901 loc) · 15.2 KB

Type Comparison Reference

The following is a list of default implementations for types that are provided in this library. Since the generation typeclass is defined on your side, you can choose a subset of the provided implementations.

In the future the following implementations may be added:

  • Uncurried Functions
  • Native tuples
  • Nonempty Arrays

Number

Number is represented as TypeScript builtin number type.

PureScript TypeScript
Ref
Number
number
Def

<builtin>

<builtin>

String

String is represented as TypeScript builtin string type.

PureScript TypeScript
Ref
String
string
Def

<builtin>

<builtin>

Boolean

Boolean is represented as TypeScript builtin boolean.

PureScript TypeScript
Ref
Boolean
boolean
Def

<builtin>

<builtin>

Array

Array is represented as TypeScript builtin ReadonlyArray type.

PureScript TypeScript
Ref
Array String
ReadonlyArray<string>
Ref
Unit -> Array a
<A>() => ReadonlyArray<A>
Def

<builtin>

<builtin>

Int

Int is represented as opaque type using TypeScript branded types. So there is no way to create an Int directly in TypeScript, you need to export a function like round :: Number -> Int and toNumber :: Int -> Number to construct and deconstruct an Int.

PureScript TypeScript
Ref
Int
import('../Prim').Int
Def

<builtin>

output/Prim/index.d.ts

type Int = {
  readonly __brand: unique symbol;
}

Maybe

Maybe is represented as opaque type using TypeScript branded types. So there is no direct way to create a Maybe in TypeScript. See the FAQ for the general decision to represent ADTs as opaque types.

PureScript TypeScript
Ref
Maybe a
import('../Data.Maybe').Maybe<A>
Def

~/Data/Maybe.purs

data Maybe a
  = Just a
  | Nothing

output/Data.Maybe/index.d.ts

type Maybe<A> = {
  readonly __brand: unique symbol;
  readonly __arg1: A;
}

Either

Either is represented as opaque type using TypeScript branded types. So there is no direct way to create a Either in TypeScript. See the FAQ for the general decision to represent ADTs as opaque types.

PureScript TypeScript
Ref
Either a b
import('../Data.Either').Either<A, B>
Def

~/Data/Either.purs

data Either a b
  = Left a
  | Right b

output/Data.Either/index.d.ts

type Either<A, B> = {
  readonly __brand: unique symbol;
  readonly __arg1: A;
  readonly __arg2: B;
}

Tuple

Tuple is represented as opaque type using TypeScript branded types. So there is no direct way to create a Either in TypeScript. See the FAQ for the general decision to represent ADTs as opaque types.

PureScript TypeScript
Ref
Tuple a b
import('../Data.Tuple').Tuple<A, B>
Def

~/Data/Tuple.purs

data Tuple a b
  = Tuple a b

output/Data.Tuple/index.d.ts

type Tuple<A, B> = {
  readonly __brand: unique symbol;
  readonly __arg1: A;
  readonly __arg2: B;
}

Nullable

From the nullable library.

Nullable is represented as TypeScript untagged union.

PureScript TypeScript
Ref
Nullable a
import('../Data.Nullable').Nullable<A>
Def

~/Data/Nullable.purs

foreign import data Nullable
  :: Type -> Type

output/Data.Nullable/index.d.ts

export type Nullable<A> = null | A

OneOf

From the untagged-union library.

OneOf is represented as TypeScript untagged union.

PureScript TypeScript
Ref
OneOf a b
import('../Untagged.Union').OneOf<A, B>
Def

~/Untagged/Union.purs

foreign import data OneOf
  :: Type -> Type -> Type

output/Untagged.Union/index.d.ts

export type OneOf<A, B> = A | B

Record

Records are represented as TypeScript records with readonly fields.

PureScript TypeScript
Ref
{ name :: String
, loggedIn :: Boolean
}
{
  readonly name: string;
  readonly loggedIn: boolean;
}
Def

<builtin>

<builtin>

Variant

From the variant library.

Variant types are represented as TypeScript tagged unions.

PureScript TypeScript
Ref
Variant
  ( done :: String
  , counting :: Number
  , init :: Unit
  )
| {
    readonly type: 'done';
    readonly value: string;
  }
| {
    readonly type: 'counting';
    readonly value: number;
  }
| {
    readonly type: 'init';
    readonly value: void;
  }
Def

~/Data/Variant.purs

foreign import data Variant
  :: Row Type -> Type

<builtin>

VariantEncFlat

From the variant-encodings library.

Flat encoded Variants are represented as TypeScript tagged unions.

PureScript TypeScript
Ref
VariantEncFlat "kind"
  ( one :: {name :: String, size :: Number}
  , two :: {hobbies :: Array String}
  )
| {
    readonly kind: 'one';
    readonly name: string;
    readonly size: number;
  }
| {
    readonly kind: 'two';
    readonly hobbies: Array<string>;
  }
Def

~/Data/Variant/Encodings/Flat.purs

foreign import data VariantEncFlat
  :: Symbol -> Row (Row Type) -> Type

<builtin>

VariantEncNested

From the variant-encodings library.

Variants with custom nested encoding are represented as TypeScript tagged unions.

PureScript TypeScript
Ref
VariantEncNested "kind" "payload"
  ( one :: Number
  , two :: String
  )
| {
    readonly kind: 'one';
    readonly payload: number;
  }
| {
    readonly kind: 'two';
    readonly payload: string;
  }
Def

~/Data/Variant/Encodings/Nested.purs

foreign import data VariantEncNested
  :: Symbol -> Symbol -> Row Type -> Type

<builtin>

VariantEncNested

From the variant-encodings library.

Variants with custom nested encoding are represented as TypeScript tagged unions.

PureScript TypeScript
Ref
VariantEncNested "kind" "payload"
  ( one :: String
  , two :: Number
  )
| {
    readonly kind: 'one';
    readonly payload: string;
  }
| {
    readonly kind: 'two';
    readonly payload: number;
  }
Def

~/Data/Variant/Encodings/Nested.purs

foreign import data VariantEncNested
  :: Symbol -> Symbol -> Row Type -> Type

<builtin>

Function

Functions are represented as TypeScript curried functions.

PureScript TypeScript
Ref
Number -> String -> Boolean
(_: number) => (_: string) => boolean
Ref
forall a b c. a -> b -> c
<A>(_: A) =>
  <B, C>(_: B) =>
    C
Def

<builtin>

<builtin>

Promise

From the aff-promise library.

Promises are represented as TypeScript Promises. Note that in most cases it makes sense to treat them as Effect (Promise a).

PureScript TypeScript
Ref
Promise a
Promise<A>
Def

~/Control/Promise.purs

foreign import data Promise :: Type -> Type

<builtin>

Effect

Effects are represented as TypeScript functions.

PureScript TypeScript
Ref
Effect a
<A>() => A
Def

~/Effect.purs

foreign import data Effect
  :: Type -> Type

<builtin>

Unit

Unit is represented as TypeScript's void

PureScript TypeScript
Ref
Unit
void
Def

~/Data/Unit.purs

foreign import data Unit
  :: Type

<builtin>