Giter VIP home page Giter VIP logo

typescript-helper's Introduction

typescript-helper

some tricky typescript utilities

Keys<T>

Keys is a Generic to get real keys and prevent keys narrow down to string|number

type OriginType = {
    name?: string;
    age?: number;
    [x:string]: any;
}

// now i want to get key of OriginType
type OriginKeys = keyof OriginType;
// oops, there is string|number ! where is "name"|"age"?

// now we use Keys to get keys
type RealOriginKeys = Keys<OriginType>;
// "name" | "age"

SafeOmit<T, K>

SafeOmit is a safe way to omit properties from ObjectType, by using Keys, Omited Type can be more accurate

type OriginType = {
    name?: string;
    age?: number;
    [x:string]: any;
}
// now i want to remove age of OriginType
type NoAgeType = Omit<OriginType, "age">;
// actual we get {[x:string]:any;[x:number]:any}

// use SafeOmit
type RealNoAgeType = SafeOmit<OriginType, "age">;
// { name?: string }

Allow<T, A>

Allow is a Generic for React ComponetProps Condition

// we need to allow more props to add in component
function Base({name, age, ...props}: {name:string;age:number}){
    return <div {...props}>{name}: {age}</div>;
}

// we can modify props type to {name: string; age: number; [x:string]: any}
const Instance = <Base name="test" age={20} className="addition-class">
// in this case, Base Component's prop type was polluted by [x:string]: any
// means, this is some props that dosen't belong to 'Base'

// or
function Base<P>({name, age, ...props}: P & Allow<{name: string; age: number}, P>){
    return <div {...props}>{name}: {age}</div>;
}
// this time, your props can keep original type, and **allow** you to control component in Instance

typescript-helper's People

Contributors

marvelsq avatar

Watchers

 avatar  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.