An open API service indexing awesome lists of open source software.

https://github.com/marvelsq/typescript-helper

some tricky typescript utilities
https://github.com/marvelsq/typescript-helper

omit typescript utilities

Last synced: 16 days ago
JSON representation

some tricky typescript utilities

Awesome Lists containing this project

README

          

# typescript-helper

some tricky typescript utilities

## Keys\

Keys is a Generic to get real keys and prevent keys narrow down to _string|number_
```ts
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;
// "name" | "age"
```

## SafeOmit

SafeOmit is a safe way to omit properties from ObjectType, by using **Keys**, Omited Type can be more accurate
```ts
type OriginType = {
name?: string;
age?: number;
[x:string]: any;
}
// now i want to remove age of OriginType
type NoAgeType = Omit;
// actual we get {[x:string]:any;[x:number]:any}

// use SafeOmit
type RealNoAgeType = SafeOmit;
// { name?: string }
```

## Allow

Allow is a Generic for React ComponetProps Condition
```ts
// we need to allow more props to add in component
function Base({name, age, ...props}: {name:string;age:number}){
return

{name}: {age}
;
}

// we can modify props type to {name: string; age: number; [x:string]: any}
const Instance =
// 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

({name, age, ...props}: P & Allow<{name: string; age: number}, P>){
return

{name}: {age}
;
}
// this time, your props can keep original type, and **allow** you to control component in Instance
```