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

TypeScript: Improve type definition to use optional chaining #244

Merged
merged 1 commit into from
Jan 20, 2020

Conversation

Kiikurage
Copy link
Contributor

Optional Chaining is the syntax in upcoming ECMAScript and introduced in TypeScript v3.7.

In #240, I(Sorry, account is different but it's me) changed react-refetch's type definition so that unsafe access can be detected by TS compiler.

render() {
  if (!this.props.userFetch.fulfilled) {
    return <span>Loading...</span>
  }

  // Here, TS compiler knows "userFetch" is "FulfilledPromiseState" instance, because "uerFetch.fulfilled" is "true"
  return <span>{ this.props.userFetch.value.name }</span>
}

However, sometimes people want to simplify this by using optional chaining

render() {
  return <span>{ this.props.userFetch.value?.name || 'Loading...' }</span>
}

This PR improve type definition so that all PromiseState type have value field to use optional chaining for generic PromiseState type.

@ryanbrainard
Copy link
Contributor

Thanks, this makes sense.

@ryanbrainard ryanbrainard merged commit 313cadd into heroku:master Jan 20, 2020
@ryanbrainard
Copy link
Contributor

Released in https://github.com/heroku/react-refetch/releases/tag/v4.0.1-0

Going to let that bake a little bit and then we can push out v4 as GA

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

2 participants