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

feat: add IsXLiteral types #563

Merged
merged 24 commits into from Mar 10, 2023

Conversation

tommy-mitchell
Copy link
Contributor

@tommy-mitchell tommy-mitchell commented Mar 6, 2023

Closes #541, closes #339.

Adds types to check if a given type is a Primitive literal:

  • IsLiteral
  • IsStringLiteral
  • IsNumericLiteral
  • IsBooleanLiteral
  • IsSymbolLiteral
  • IsNullLiteral
  • IsUndefinedLiteral

Marking as draft as I still need to add documentation comments to the types.

Info About Removed Types

I'm not sure if it's possible to check for null or undefined literals without also allowing their values, e.g:

import type {IsNullLiteral} from 'type-fest';

declare const _null: null;

type ShouldBeTrue = IsNullLiteral<null>;
//=> true

type ShouldBeFalse = IsNullLiteral<typeof _null>;
//=> true

Marking as draft as I still need to figure out the above issue and add documentation comments to the types. Currently the test cases for _null and _undefined values are commented out.

@tommy-mitchell
Copy link
Contributor Author

Thinking about it some more, I’m not sure if there’s even really a reason to check for null or undefined literals, beyond completeness sake. And the more I think about it, I don’t think it’s possible to either.

source/is-literal.d.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

@sindresorhus
Copy link
Owner

Lets just drop null and undefined literal. I cannot think of any use-case for them.

@tommy-mitchell
Copy link
Contributor Author

Should the category be Utilities?

@tommy-mitchell
Copy link
Contributor Author

Had to force push because I committed on the wrong user.

@tommy-mitchell
Copy link
Contributor Author

Currently, IsLiteral has a Primitive type constraint:

type IsLiteral<T extends Primitive> = // ...

This means that testing types that aren't assignable to Primitive (e.g. unknown) is a compiler error:

type ShouldBeFalse = IsLiteral<unknown>;
//=> Type 'unknown' does not satisfy the constraint 'Primitive'.

The other IsXLiteral types don't have this constraint. Should this be removed?

@tommy-mitchell
Copy link
Contributor Author

Seems like these aren't being used as type guards either. The following errors at Capitalize<T>:

type CapitalizedString<T> = IsStringLiteral<T> extends true ? Capitalize<T> : string;

// (type parameter) T in type CapitalizedString<T>
// Type 'T' does not satisfy the constraint 'string'. ts(2344)

Not sure what I'm doing wrong.

@tommy-mitchell
Copy link
Contributor Author

Currently just missing examples for IsBooleanLiteral and IsSymbolLiteral. From searching on GitHub (the new code search is actually quite nice), the only real uses I've seen for the two are when building parsers/ASTs.

@tommy-mitchell tommy-mitchell marked this pull request as ready for review March 9, 2023 02:24
@tommy-mitchell
Copy link
Contributor Author

All types are now documented with examples.

@sindresorhus
Copy link
Owner

Should the category be Utilities?

👍 And maybe also a new one: Is or Type Guard?

@tommy-mitchell
Copy link
Contributor Author

👍 And maybe also a new one: Is or Type Guard?

IsType?

@sindresorhus
Copy link
Owner

IsType?

Makes it sound like it's a special type, not a type of type. Let's go with Type Guard for now.

@sindresorhus sindresorhus merged commit 9d00bac into sindresorhus:main Mar 10, 2023
7 checks passed
@sindresorhus
Copy link
Owner

Looks great :)

tommy-mitchell added a commit to tommy-mitchell/type-fest that referenced this pull request Mar 10, 2023
orimiles5 pushed a commit to orimiles5/type-fest that referenced this pull request Mar 11, 2023
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

Successfully merging this pull request may close these issues.

Feature request: IsStringLiteral Proposal: IsLiteral
2 participants