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

What are Utility Types? #1

Open
quangv opened this issue Oct 21, 2022 · 1 comment
Open

What are Utility Types? #1

quangv opened this issue Oct 21, 2022 · 1 comment

Comments

@quangv
Copy link

quangv commented Oct 21, 2022

TypeScript provides several utility types to facilitate common type transformations.

https://www.typescriptlang.org/docs/handbook/utility-types.html#excludeuniontype-excludedmembers

To me these seems like type functions? But I don't think functions is the right word.

type T0 = Exclude<"a" | "b" | "c", "a">;
// type T0 = "b" | "c"

Is utility types the right nomenclature in TypeScript world?

Utility types = tools that transform types? type transformers?

Exclude<UnionType, ExcludedMembers>

// Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers.

Utility types = type constructors?

@ruizb
Copy link
Owner

ruizb commented Oct 22, 2022

Hello 🙂

Yes, "utility type" is the right terminology as it comes directly from the official TypeScript website, per the link you shared.

Utility types are mapped types and conditional types that are defined and exposed from TypeScript itself (cf. lib.es5.d.ts). They are called "utility" because they are great at doing common operations on types.

You can define your own utility types in your projects depending on your needs, as "utility" is just a label put on top of a group of mapped and conditional types.

To me these seems like type functions? But I don't think functions is the right word.

This is actually a great way of seeing types that take other types as parameter(s). Any type that has at least 1 type parameter can be considered a "function at the type-level". But generally, these types are called "generic types" instead of "functions" or "type transformers".

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