Skip to content

Use TS assertion signature to avoid type casting#30

Merged
francisrstokes merged 1 commit into
francisrstokes:mainfrom
kubk:ts-assertion
Jan 9, 2022
Merged

Use TS assertion signature to avoid type casting#30
francisrstokes merged 1 commit into
francisrstokes:mainfrom
kubk:ts-assertion

Conversation

@kubk
Copy link
Copy Markdown
Contributor

@kubk kubk commented Jan 9, 2022

Starting from TS 3.7 it's possible to use so-called "assertion signatures" to narrow TS types after guard statements. It's described here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions

It allows to avoid type casting. For some reason assertion signatures are available only for function keyword, that's why I had replace const with function

  • This PR only introduces changes to documentation.
    • No
  • This PR adds new functionality
    • Is there a related issue?
      • No, it's just a an improvement utilizing latest TS features
    • Does the code style reasonably match the existing code?
      • Yes
    • Are the changes tested (using the existing format, as far as is possible?)
      • Yes
    • Are the changes documented in the readme with a suitable example?
      • Not needed
    • Is the table of contents updated?
      • Not needed
    • Is the index.d.ts file updated, using the same (or appropriately modified) description as in the readme?
      • Not needed
@francisrstokes francisrstokes merged commit 2ca1851 into francisrstokes:main Jan 9, 2022
@francisrstokes
Copy link
Copy Markdown
Owner

Definitely an improvement, thanks @kubk! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants