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
- Host: GitHub
- URL: https://github.com/marvelsq/typescript-helper
- Owner: MarvelSQ
- License: mit
- Created: 2021-05-11T08:08:11.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-31T08:50:17.000Z (over 4 years ago)
- Last Synced: 2025-03-17T04:43:10.475Z (about 1 year ago)
- Topics: omit, typescript, utilities
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```