New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simplytyped

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simplytyped

yet another Typescript type library for advanced types

  • 0.1.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.8K
decreased by-50.99%
Maintainers
1
Weekly downloads
 
Created
Source

SimplyTyped

Build Status

Yet another typing library. This differs by aiming to be less experimental than others, driven by industry use cases.

Many of the exposed types are a very thin layer above built in functionality. The goal is to provide all of the building blocks necessary to make concise, yet complex types.

Additionally packaged with this lib is a JSON-schema validator that will act as a type-guard for appropriately typed object. Using this will mean run-time checking of user/network/external data, with compile time checking of logic flow. The primary purpose of packaging the JSON-schema validator is as an example of complexity of types this library is able to specify. In particular, this shows the library's solution to the conditional mapped types problem without overloading the global namespace.

npm install --save-dev simplytyped

Conditionals

If - And - Or - Not - Xor - Nand

Predicates

IsAny - IsArray - IsBoolean - IsFunction - IsNever - IsNumber - IsObject - IsType

Objects

Keys - ObjectType - CombineObjects - Intersect - SharedKeys - DiffKeys - AllKeys - Omit - Merge - Overwrite - DeepPartial - DeepReadonly - Optional - GetKey

Tuples

Tuple - UnionizeTuple - Length

Strings

Diff - StringEqual - DropString

Numbers

IsZero - IsOne - NumberToString - Next - Prev - Add - Sub - NumberEqual

Functions

Predicate - ConstructorFunction

Schema Validation

Conditionals

Some (somewhat common) implementations of conditional type logic

If

type x = If<True, string, number> // => string
type y = If<False, string, number> // => number

And

type x = If<And<True, True>, string, number> // => string
type y = If<And<True, False>, string, number> // => number
...

Or

type x = If<Or<True, False>, string, number> // => string
type y = If<Or<False, False>, string, number> // => number
...

Not

type x = Not<True> // => False
type y = Not<False> // => True

Xor

type x = Xor<True, False> // => True
type y = Xor<True, True> // => False
...

Nand

type x = Nand<True, True> // => False
type y = Nand<False, True> // => True

Predicates

IsAny

type x = IsAny<any> // => True
type y = IsAny<'hey'> // => False

IsArray

type x = IsArray<any[]> // => True
type y = IsArray<number> // => False

IsBoolean

type x = IsBoolean<false> // => True
type y = IsBoolean<3> // => False

IsFunction

type x = IsFunction<(() => string)> // => True
type y = IsFunction<'not a function'> // => False

IsNever

Returns true if type is never, otherwise returns false.

type x = IsNever<'hi'> // => False
type y = IsNever<never> // => True

IsNumber

type x = IsNumber<3> // => True
type y = IsNumber<false> // => False

IsObject

type x = IsObject<{a: number, b: string}> // => True
type y = IsObject<string> // => False

IsType

Given a base type and a value, check to see if value matches the base type. Useful for checking if something is an instance of a class.

class Thing { x: string };
type x = IsType<Thing, { x: string }> // => True
type y = IsType<Thing, { y: number }> // => False

Objects

type obj1 = { w: string, x: string, y: number }
type obj2 = { y: string, z: number }

Keys

No different than keyof, but can look a bit nicer when nesting many types deep

type x = keyof obj1 // => 'w' | 'x' | 'y'
type y = Keys<obj1> // => 'w' | 'x' | 'y'

ObjectType

On its own, not that interesting. Takes an object and makes it an object type. Is useful when combined with & intersection types (as seen next).

type x = ObjectType<obj1> // => { w: string, x: string, y: number }

CombineObjects

Takes the intersection between two objects, and flattens them. This can make extremely complex types look much nicer.

type x = obj1 & obj2 // => { w: string, x: string, y: number } & { y: string, z: number }
type y = CombineObjects<obj1, obj2> // => { w: string, x: string, y: string & number, z: number }

Intersect

Returns only the shared properties between two objects. All shared properties must be the same type.

type x = Intersect<obj1, { x: string }> // => { x: string }

SharedKeys

Gets all of the keys that are shared between two objects (as in keys in common).

type x = SharedKeys<obj1, obj2> // => 'y'

DiffKeys

Gets all of the keys that are different from obj1 to obj2.

type x = DiffKeys<obj1, obj2> // => 'w' | 'x'
type y = DiffKeys<obj2, obj1> // => 'z'

AllKeys

Gets all keys between two objects.

type x = AllKeys<obj1, obj2> // => 'w' | 'x' | 'y' | 'z'

Omit

Gives back an object with listed keys removed.

type x = Omit<obj1, 'w' | 'x'> // => { y: number }

Merge

Much like _.merge in javascript, this returns an object with all keys present between both objects, but conflicts resolved by rightmost object.

type x = Merge<obj1, obj2> // => { w: string, x: string, y: string, z: number }

Overwrite

Can change the types of properties on an object. This is similar to Merge, except that it will not add previously non-existent properties to the object.

type a = Overwrite<obj1, obj2> // => { w: string, x: string, y: string }
type b = Overwrite<obj2, obj1> // => { y: number, z: number }

DeepPartial

Uses Partial to make every parameter of an object optional (| undefined).

type x = DeepPartial<obj1> // => { w?: string, x?: string, y?: number }

DeepReadonly

Uses Readonly to make every parameter of an object readonly

type x = DeepReadonly<obj1> // => { w: readonly string, x: readonly string, y: readonly number }

Optional

Makes certain properties on an object optional.

type x = Optional<obj1, 'w' | 'x'> // => { w?: string, x?: string, y: number }

GetKey

Gets the value of specified property on any object without compile time error (Property 'b' does not exist on type '{ a: string; }'.) and the like. Returns never if the key is not on the object. I suggest using If<HasKey... first to handle validity of the object first.

type x = GetKey<{ a: string }, 'a'> // => string
type y = GetKey<{ a: string }, 'b'> // => never

Tuples

A tuple can be defined in two ways: [number, string] which as of Typescript 2.7 has an enforced length type parameter: [number, string]['length'] === 2 or using this library's Tuple<any> which can be extended with any length of tuple: function doStuff<T extends Tuple<any>>(x: T) {}.

Tuple

function doStuff<T extends Tuple<any>>(x: T) {}

doStuff(['hi', 'there']); // => doStuff(x: ['hi', 'there']): void

UnionizeTuple

Returns elements within a tuple as a union.

type x = UnionizeTuple<[number, string]> // => number | string

Length

Gets the length of either a built-in tuple, or a Vector. This will only work after Typescript 2.7 is released.

type x = Length<['hey', 'there']> // => 2

Strings

Diff

Get the differences between two unions of strings.

type x = Diff<'hi' | 'there', 'hi' | 'friend'> // => 'there'

StringEqual

Returns true if all elements in two unions of strings are equal.

type x = StringEqual<'hi' | 'there', 'hi'> // => False
type y = StringEqual<'hi' | 'there', 'there' | 'hi'> // => True

DropString

Can remove a string from a union of strings

type x = DropString<'hi' | 'there', 'hi'> // => 'there'

Numbers

Supports numbers from [0, 63]. More slows down the compiler to a crawl right now.

IsZero

Returns true if the number is equal to zero.

type x = IsZero<1> // => False
type y = IsZero<0> // => True

IsOne

Returns true if the number is equal to one.

type x = IsOne<0> // => False
type y = IsOne<1> // => True

NumberToString

Returns the string type for a given number

type x = NumberToString<0> // => '0'
type y = NumberToString<1> // => '1'

Next

Returns the number + 1.

type x = Next<0> // => 1
type y = Next<22> // => 23

Prev

Returns the number - 1.

type x = Prev<0> // => -1
type y = Prev<23> // => 22

Add

Adds two numbers together.

type x = Add<22, 8> // => 30

Sub

Subtracts the second from the first.

type x = Sub<22, 8> // => 14

NumberEqual

Returns True if the numbers are equivalent

type x = NumberEqual<0, 0> // => True
type y = NumberEqual<22, 21> // => False

Functions

Predicate

This is a function that takes some args and returns a boolean

type x = Predicate // => (...args: any[]) => boolean
const isThing: Predicate = (x: Thing) => x instanceof Thing;

ConstructorFunction

This represents the constructor for a particular object.

class Thing { constructor(public x: string) {}}
type x = ConstructorFunction<Thing>
declare const construct: x;
const y = new construct(); // => y instanceof Thing

Schema Validation

One of the easiest points of failure with the typescript type system is outside data. It is difficult to confirm that outside data matches the contract we have set within our typings. Using the common JSON-schema specification, we can check the validity of our runtime objects, while still having compile checking of logic validity. If we define our schemas as so:

const schema = {
    type: 'object' as 'object',
    properties: {
        prop: {
            type: 'string' as 'string'
        }
    }
}

then pass in data:

// use `any` to pretend we don't know the type here.
const runtimeData: any = {
    prop: 'hello world'
};

then schemaIsValid(data, schema) will give both compile time and run time checking of types.

if (schemaIsValid(data, schema)) {
    type x = typeof data; // => { prop: string }
} else {
    throw new Error('Uh-oh, you gave me ill-formatted data!!');
}

Keywords

FAQs

Package last updated on 25 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc