Coder Social home page Coder Social logo

liam-scott-russell / magic-ts Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 1.88 MB

Typescript types so good they're magic!

License: Apache License 2.0

TypeScript 99.80% Shell 0.20%
typed types typescript typescript-definitions typescript-library utility utility-library utility-types

magic-ts's Introduction

magic-ts

Typescript types so good they’re magic!

Conventions

Naming

Predicate Type Functions

A predicate type function is a type that takes at least three generics, where the final two generic types are called OnTrue and OnFalse. OnTrue will always default to Conditional.True, and OnFalse will always default to Conditional.False, meaning that they are not required. A predicate type function will always return Conditional.True or Conditional.False (i.e. Conditional.Any).

An example of this is the Inheritance.IsExtensionOf predicate type function, which takes two parameters ExtendedType, BaseType and returns whether ExtendedType extends BaseType:

export type IsExtensionOf<
  ExtendedType,
  BaseType,
  OnTrue = Conditional.True,
  OnFalse = Conditional.False
> = ExtendedType extends BaseType ? OnTrue : OnFalse;

// Not necessary to provide values for OnTrue and OnFalse
type WithDefaults = IsExtensionOf<1, number>; // Conditional.True

//Not necessary to provide values for OnFalse when providing a value for OnTrue
type WithDefaultOnTrue = IsExtensionOf<1, number, 2>; // 2

//Can provide any value for OnTrue and OnFalse
type ProvidingValues = IsExtensionOf<
  "magic-ts",
  string,
  "yes",
  { result: "no" }
>; // "yes"

// Can nest predicate calls inside each other
type Nested = IsExtensionOf<
  1,
  number,
  IsExtensionOf<
    1,
    string,
    "is a number, and a string",
    "is a number, not a string"
  >,
  "not a number, not a string"
>; // "is a number, not a string"

Getter Type Functions

A getter type function is a type that always takes at least two generic, where the final generic type is called Default. Default will always default to never, meaning that it is not required. A getter function will “get” some value, and if it cannot (for a variety of reasons), then it will return Default.

type Tuple__Head<T extends Tuple.Any, Default = never> = T extends [
  infer Head,
  ...infer _Tail
]
  ? Head
  : Default;

type Head = Tuple__Head<[1, 2, 3]>; // 1
// an empty tuple has no head
type HeadDefault = Tuple__Head<[], "SomeDefaultValue">; // "SomeDefaultValue"

export type Struct__Get<
  TStruct extends Struct.Any,
  Key extends Struct.KeysAllowed,
  Default = never
> = Key extends keyof TStruct ? TStruct[Key] : Default;

type Value = Struct__Get<
    { a: "aValue", b: "bValue" },
    "a"
>; // "aValue"

type ValueDefault = Struct__Get<
    { a: "aValue", b: "bValue" },
    "c",
    "SomeDefaultValue"
>; // "SomeDefaultValue"

magic-ts's People

Contributors

liam-scott-russell avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.