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 State generic to field types #13

Merged
merged 3 commits into from
Oct 11, 2021
Merged

feat: add State generic to field types #13

merged 3 commits into from
Oct 11, 2021

Conversation

angeloashmore
Copy link
Member

@angeloashmore angeloashmore commented Oct 6, 2021

Types of changes

  • Chore (a non-breaking change which is related to package maintenance)
  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This PR adds a State generic to field types that can be empty. For example, a Select field can be empty which returns null rather than a string.

In the following example, State is the second generic.

import * as prismicT from "@prismicio/types"

type MyField = prismicT.SelectField<"Option 1" | "Option 2", "empty">
// MyField === null

To explicitly set the field as "filled":

import * as prismicT from "@prismicio/types"

type MyField = prismicT.SelectField<"Option 1" | "Option 2", "filled">
// MyField === "Option 1" | "Option 2"

By default, the state is ambiguous (i.e. "empty" | "filled"):

import * as prismicT from "@prismicio/types"

type MyField = prismicT.SelectField<"Option 1" | "Option 2">
// MyField === "Option 1" | "Option 2" | null

Related @prismicio/mock PR: prismicio/prismic-mock#4

Checklist:

  • My change requires an update to the official documentation.
  • All TSDoc comments are up-to-date and new ones have been added where necessary.
  • All new and existing tests are passing.

🦮

@codecov-commenter
Copy link

codecov-commenter commented Oct 6, 2021

Codecov Report

Merging #13 (460f466) into master (b075b39) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #13   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines           59        59           
=========================================
  Hits            59        59           
Impacted Files Coverage Δ
src/fields.ts 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b075b39...460f466. Read the comment docs.

@angeloashmore angeloashmore changed the title feat: add IsEmpty generic to emptyable field types feat: add IsEmpty generic to emptyable field types Oct 6, 2021
Copy link
Member

@lihbr lihbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, that's quite a weird way of doing a "union if" but seems like the only way ^^'

Maybe you can add a short TSDoc for the isEmpty type params to explain it (others can be postponed for now)

If boolean is still too obscure have you considered using a string type? Something like this (not as nice maybe but I actually quite like it, and gives the "filled" state as a bonus ☺️):

// Union let a hint of what's expected(?)
export type ImageFieldImage<State extends "empty" | "filled" = "empty" | "filled"> =
	State extends "empty"
		? EmptyImageFieldImage
		: FilledImageFieldImage;

Schrodinger's types 🤔

@angeloashmore
Copy link
Member Author

angeloashmore commented Oct 6, 2021

Great idea! IsEmpty has been replaced by FieldState:

import * as prismicT from '@prismicio/types'

type MyField = prismicT.SelectField<"Option 1" | "Option 2", "empty">
// MyField === null

I'll add TSDocs for the new generic before considering this ready to merge.

@angeloashmore angeloashmore marked this pull request as draft October 7, 2021 04:42
@angeloashmore angeloashmore changed the title feat: add IsEmpty generic to emptyable field types feat: add State generic to emptyable field types Oct 11, 2021
@angeloashmore angeloashmore marked this pull request as ready for review October 11, 2021 20:21
@angeloashmore angeloashmore changed the title feat: add State generic to emptyable field types feat: add State generic to field types Oct 11, 2021
@angeloashmore angeloashmore merged commit 368d968 into master Oct 11, 2021
@angeloashmore angeloashmore deleted the aa/is-empty branch October 11, 2021 20:21
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.

None yet

3 participants