npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hyperapp-types

v0.3.0

Published

useful and strict type definitions for TypeScript with hyperapp

Downloads

3

Readme

hyperapp-types

Useful and strict type definitions for TypeScript with hyperapp.

Requirement

hyperapp V1 (1.x.x) and TypeScript 2.8 later

Features

hyperapp-types includes 2 utility types.

  1. WiredActions - Generate wired actions type from actions. (Also nesting action)
  2. ParamType - Extract parameter type from action. It is a little something extra :-)

Installation

Install with npm / Yarn.

npm install hyperapp-types

Usage

1. WiredActions

Pass the state type and the actions type to WiredActions. WiredActions will generate correctly typed actions types based on them.

import { WiredActions } from "hyperapp-types";

interface MyState {
  count: number;
}

const state: MyState = {
  count: 0
};

const actionImplements = {
  down:     (value: number) => (state: MyState, actions: MyActions) => ({ count: state.count - value }),
  up:       (value: number) => (state: MyState) => ({ count: state.count + value }),
  reset:    () => ({ count: 0 }),
  getState: () => (state: MyState) => (state)
};

//------------------------
// without hyperapp-types
//------------------------
const view1 = (state: MyState, actions: (typeof actionImplements)) => {
    actions.down(5); // OK
    actions.reset(); // OK

    let st = actions.getState();  // TypeScript infers that `st` is function - ((state: MyState) => MyState)  But, st is MyState object exactly.
    // console.log(st.count); // TypeScript raises a build error

    // You may write for avoiding TypeScript build error, but it is verbose and unsafe...
    // (if getState() returns partial state or Promise?)
    let st2 = ((actions.getState() as any) as MyState);
    console.log(st2.count); // Build OK
};

//------------------------
// with hyperapp-types
//------------------------
type MyActions = WiredActions<MyState, (typeof actionImplements)>; // generate type of wired actions from `actionImplements`

const view2 = (state: MyState, actions: MyActions) => {
    actions.down(5); // OK
    actions.reset(); // OK

    let st = actions.getState(); // TypeScript infers that `st` is MyState. It is correct and typesafe!
    console.log(st.count); // OK
};

hyperapp-types also supports nested actions.

import { WiredActions } from "hyperapp-types";

interface MyState {
  label: {
    text: string;
  }
}

const state: MyState = {
  label: {
    text: ""
  }
};

type LabelState = MyState['label'];
type LabelActions = MyActions['label'];

const actionImplements = {
  label: {
    setText: (data: string) => ({text: ''}),
    getLabel: () => (state: LabelState, actions: LabelActions) => (state)
  }
};

type MyActions = WiredActions<MyState, (typeof actionImplements)>;

const view2 = (state: MyState, actions: MyActions) => {
    actions.label.setText('1');
    let currentLabelText = actions.label.getLabel().text;
};

2. ParamType

ParamType extracts parameter types from function type with zero or one parameter (action function).

import { ParamType, WiredActions } from "hyperapp-types";

interface MyState {
  count: number;
}

const state: MyState = {
  count: 0
};

const actionImplements = {
  down:     (value: number) => (state: MyState, actions: MyActions) => ({ count: state.count - value }),
  reset:    () => ({ count: 0 })
};

type MyActions = WiredActions<MyState, (typeof actionImplements)>; // generate type of wired actions from `actionImplements`


const view1 = (state: MyState, actions: MyActions) => {
  // Get parameter type from actions
  type paramType = ParamType<typeof actions.down>; // paramType is number
  // or
  // type paramType = ParamType<MyActions['down']>;
  // type paramType = ParamType<typeof actionImplements.down>;

  // If an action hasn't any parameter, you will get never type of TypeScript.
  type resetParamType = ParamType<typeof actions.reset>; // resetParamType is never
};

License

Unlicensed

Contact

@tetradice (Twitter)