Skip to content

Commit f950f3e

Browse files
committed
Replace as assertions with redundant type guards
Sometimes TypeScript's type narrowing and inference doesn't fully work quite like it should. Even in these cases, using `as` assertions should be avoided. It may seem like we know for certain what the type should be and there's no risk of it breaking, but there's always the possibility that some combination of changes elsewhere might affect the typing, in which case `as` will suppress the alerts TypeScript would otherwise provide us with. This may lead to code breaking silently. In this case, adding type guards and null checks -- even if they're redundant -- is preferable to the above scenario. - TODO: document and expound in MetaMask/contributor-docs#47
1 parent 53cf673 commit f950f3e

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/update-changelog.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export async function updateChangelog({
217217

218218
if (
219219
isReleaseCandidate &&
220-
mostRecentTag === `${tagPrefixes[0]}${currentVersion || ''}`
220+
mostRecentTag === `${tagPrefixes[0]}${currentVersion ?? ''}`
221221
) {
222222
throw new Error(
223223
`Current version already has tag, which is unexpected for a release candidate.`,
@@ -250,19 +250,16 @@ export async function updateChangelog({
250250
// Ensure release header exists, if necessary
251251
if (
252252
isReleaseCandidate &&
253+
currentVersion &&
253254
!changelog
254255
.getReleases()
255256
.find((release) => release.version === currentVersion)
256257
) {
257-
// Typecast: currentVersion will be defined here due to type guard at the
258-
// top of this function.
259-
changelog.addRelease({ version: currentVersion as Version });
258+
changelog.addRelease({ version: currentVersion });
260259
}
261260

262-
if (isReleaseCandidate && hasUnreleasedChanges) {
263-
// Typecast: currentVersion will be defined here due to type guard at the
264-
// top of this function.
265-
changelog.migrateUnreleasedChangesToRelease(currentVersion as Version);
261+
if (isReleaseCandidate && currentVersion && hasUnreleasedChanges) {
262+
changelog.migrateUnreleasedChangesToRelease(currentVersion);
266263
}
267264

268265
const newChangeEntries = newCommits.map(({ prNumber, description }) => {

0 commit comments

Comments
 (0)