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

deep-type-safe

v0.0.7

Published

Generic type for deep path string

Downloads

2

Readme

Deep type safe

NPM NPM NPM Typescript

Deep-type-safe is generic types for function using nested path string.

Remove all any type from your project and enjoy TypeScript!😁

// == with lodash ==
// lodash `get` will return `any` type when path contains dot or bracket
const thisIsAny = _.get({ nested: { path: 0 } }, 'nested.path');
// Wrap lodash function with PathToType and you will get type-safe value!
const thisIsNumber = get({ nested: { path: 0 } }, 'nested.path');

Install

deep-type-safe works on TypeScript^4.1 because it uses Template Literal Types.

$ npm install deep-type-safe

or

$ yarn add deep-type-safe

Usage

You can use nested path string with type-safe.

Examples

Simple Examples

// nested object
type prop = PathToType<{ a: number }, 'a'>; // type prop = number
type child = PathToType<{ a: { b: number } }, 'a'>; // type child = { b: number; }
type nested = PathToType<{ a: { b: number } }, 'a.b'>; // type nested = number

// array type
type array = PathToType<string[], '[0]'>; // type array = string
type matrix = PathToType<string[][], '[1][2]'>; // type matrix = string

// optional
type optional = PathToType<
    { nested?: { optional: number } },
    'nested.optional'
>; // type optional = number | undefined

Type-safe Parameter

// with Formik
import { useFormikContext } from 'formik';
import { PathToType } from "deep-type-safe";

type YourForm = {
    with: {
        nested: {
            num: number;
            str: string;
        };
    };
};
export const MyInput = () => {
    const { setFieldValue: _setFieldValue } = useFormikContext<YourForm>();

    // Wrap formik with PathToType and get type-safe methods
    const setFieldValue = <P extends string>(
        field: P,
        value: PathToType<YourForm, P>,
        shouldValidate?: boolean
    ): void => _setFieldValue(field, value, shouldValidate);

    // OK😀
    const onChange = (value: string) => setFieldValue('with.nested.str', value);

    // Type error!🔥
    const invalidValue = (value: string) => setFieldValue('with.nested.num', value);
    const invalidPath = (value: string) => setFieldValue('invalid.path', value);

Type-safe return value

// with lodash
import _ from 'lodash';
import { PathToType } from 'deep-type-safe';

type YourType = {
    with: {
        nested: {
            property: number;
        };
    };
};
const obj = { with: { nested: { property: 1 } } };

// Wrap lodash with PathToType
const get = <P extends string, T, D = never>(
    object: T,
    path: P,
    defaultValue?: D
): PathToType<T, P, D> => _.get(object, path, defaultValue);

// number😀
const nestedValue = get(obj, 'with.nested.property');

// never🔥
const invalid = get(obj, 'invalid.path');