Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggesting some useful Utility Types #45178

Closed
5 tasks done
HADMARINE opened this issue Jul 25, 2021 · 2 comments
Closed
5 tasks done

Suggesting some useful Utility Types #45178

HADMARINE opened this issue Jul 25, 2021 · 2 comments

Comments

@HADMARINE
Copy link

HADMARINE commented Jul 25, 2021

Suggestion

πŸ” Search Terms

Util types, Utility types, etc...

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I've made some utility types similar to existing types
It is useful, so I am using it on every typescript projects by declaring on global namespace.

I will introduce one by one.

ValueOf

This type returns all of the type of value of object(Record)

type ValueOf<T> = T[keyof T]; 


//example

const a: Record<string, number | string> = {};

type valueof_a = ValueOf<typeof a>; // number | string

TypeGuard

This type returns type that the typeguard is guarding

  type TypeGuard<T extends (args: any) => any> = T extends (
    args: any,
  ) => args is infer R
    ? R
    : any;


//example

function stringGuard(data: any): data is string {
  return typeof data === 'string';
}

type string_guardingType = TypeGuard<typeof stringGuard>; //string


function numberGuard(data:any) : data is number {
    return typeof data === 'number';
}

type number_guardingType = TypeGuard<typeof numberGuard> //number

Nullish

This type adds null to all of the values of object

  type Nullish<T> = {
    [P in keyof T]: T[P] | null;
  };

//example

type objType = { a: number; b: string };

type objNullable = Nullish<objType>;
// {
//     a: number | null;
//     b: string | null;
// }

Arrayify

This type arrayifies all the object's value.

  type Arrayify<T> = {
    [P in keyof T]: T[P][];
  };


//example


const obj = { hello: 'world', a: 2 };

type objType = Arrayify<typeof obj>; 
// {
//    hello: string[];
//    a: number[];
// }

If you decided to add this but the name seems to be inappropriate, I won't mind to change the name of types. But let me know the new name.

πŸ“ƒ Motivating Example

This improves the usability of the language. It can be used by creating like I did, but it would be better when it is already built-in.

πŸ’» Use Cases

I've written all the examples above.

@MartinJohns
Copy link
Contributor

See #39522 (comment):

We've opted to not include utility type aliases in the lib unless they're required for declaration emit purposes.

@HADMARINE
Copy link
Author

Okay, I'll close this issue. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants