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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use unreachable exception in favor of type safety #22933

Merged
merged 3 commits into from Apr 5, 2020

Conversation

fa93hws
Copy link
Contributor

@fa93hws fa93hws commented Apr 5, 2020

馃 This is a ...

  • New feature
  • Bug fix
  • Site / document update
  • Component style update
  • TypeScript definition update
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

馃懟 What's the background?

Switch is not type safe.
e.g.

type Pet = 'dog' | 'cat';

function getPet(pet: Pet) {
  switch(pet) {
    case 'dog':
        return new Dot();
    case 'cat':
        return new Cat();
    default:
        throw new Error('unknown pet');
  }
}

Which looks good now.

However, if another value is added to the enum, e.g. type Pet = 'dog' | 'cat' | 'hamster'.
The getPet function stay silent in the compile time.
It may (or may not) throw an error in the run time which is error prone.

馃挕 Solution

It is possible to utilize the never type in typescript to force the switch statement to behave like an exhaustive pattern match to enhance the type safety.

B6A8D1EA-D2E6-4901-BE05-FC9A526B9C57

All occurrence will be changed if the gist of this PR is preferred.

馃摑 Changelog

Language Changelog
馃嚭馃嚫 English
馃嚚馃嚦 Chinese

鈽戯笍 Self Check before Merge

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

Copy link

@tests-checker tests-checker bot left a comment

Could you please add tests to make sure this change works as expected?

@fa93hws fa93hws changed the title Fa93hws/unreachable Use unreachable exception in favor of type safety Apr 5, 2020
@fa93hws fa93hws changed the title Use unreachable exception in favor of type safety chore: Use unreachable exception in favor of type safety Apr 5, 2020
break;
default:
console.warn(new UnreachableException(size));
Copy link
Contributor Author

@fa93hws fa93hws Apr 5, 2020

Choose a reason for hiding this comment

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

Suggested change
console.warn(new UnreachableException(size));
console.warn(new UnreachableException(size));
break;

@ant-design-bot
Copy link
Contributor

ant-design-bot commented Apr 5, 2020

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 5, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 75136e0:

Sandbox Source
antd reproduction template Configuration

@codecov
Copy link

codecov bot commented Apr 5, 2020

Codecov Report

Merging #22933 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #22933   +/-   ##
=======================================
  Coverage   98.31%   98.31%           
=======================================
  Files         363      364    +1     
  Lines        7253     7258    +5     
  Branches     1978     1991   +13     
=======================================
+ Hits         7131     7136    +5     
  Misses        122      122           
Impacted Files Coverage 螖
components/_util/unreachableException.ts 100.00% <100.00%> (酶)
components/button/button-group.tsx 100.00% <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 4e20ed2...75136e0. Read the comment docs.

@afc163
Copy link
Member

afc163 commented Apr 5, 2020

Could you add test case?

@fa93hws
Copy link
Contributor Author

fa93hws commented Apr 5, 2020

Could you add test case?

馃憣
75136e0

@afc163 afc163 merged commit 316b925 into ant-design:master Apr 5, 2020
16 checks passed
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