SemVer: narrow isPrerelease to SemVer.Prerelease and reject sparse identifier arrays

オープン
#11,048 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@effect-bot がすでに取り組んでいます。

2026年11月29日 から。

  • #11049 @jasonkuhrt による — オープン

評価

この issue はまだ評価されていません。

説明

Written by an AI agent (Antigravity) on behalf of @jasonkuhrt.

Make SemVer.isPrerelease a type guard ((self: SemVer) => self is Prerelease) narrowing prerelease to Array.NonEmptyReadonlyArray<string | number>, export the SemVer.Prerelease interface, and reject sparse identifier arrays in SemVer.make, withPrerelease, and withBuild.

Context

effect/SemVer (added in #9243, currently @stability unstable in [email protected] and on main at 584c9e090) defines isPrerelease as returning plain boolean:

export const isPrerelease = (self: SemVer): boolean => self.prerelease.length > 0

Because it does not narrow, callers who have already checked SemVer.isPrerelease(version) still see version.prerelease as ReadonlyArray<string | number>, so non-empty operations fail to type-check without a redundant check:

import { Array, SemVer } from "effect"

const version = SemVer.parseUnsafe("2.0.0-rc.1")

if (SemVer.isPrerelease(version)) {
  // TS2345 on [email protected] and main (584c9e090):
  // Argument of type 'readonly (string | number)[]' is not assignable to
  // parameter of type 'readonly [string | number, ...(string | number)[]]'
  Array.headNonEmpty(version.prerelease)
}

In addition, SemVer.make, withPrerelease, and withBuild currently validate identifier arrays with .map(...), which preserves sparse array holes: passing new Array<string>(1) produces a SemVer where isPrerelease is true, prerelease[0] is undefined, and SemVer.format returns "1.0.0-". Validating indexed entries with an explicit loop and a typeof identifier !== "string" check rejects sparse holes so Prerelease.prerelease is sound at runtime too.

Proposed change

Export Prerelease as a sub-interface of SemVer and narrow isPrerelease to it:

export interface Prerelease extends SemVer {
  readonly prerelease: NonEmptyReadonlyArray<string | number>
}

export const isPrerelease = (self: SemVer): self is Prerelease =>
  self.prerelease.length > 0

With this change:

import { Array, SemVer } from "effect"

const versions = ["1.0.0", "2.0.0-rc.1", "2.0.0-beta.3"].map(SemVer.parseUnsafe)

// Array<SemVer.Prerelease> -> ["rc", "beta"]
Array.filter(versions, SemVer.isPrerelease).map((version) =>
  Array.headNonEmpty(version.prerelease)
)
Compatibility

isPrerelease remains assignable to (self: SemVer) => boolean and Prerelease is assignable to SemVer, but narrowing changes the inferred return type of calls like Array.filter(versions, SemVer.isPrerelease) from Array<SemVer.SemVer> to Array<SemVer.Prerelease>. Code that mutates that array by pushing an un-narrowed SemVer (e.g. from SemVer.parseUnsafe) fails with TS2345 unless annotated as Array<SemVer.SemVer>. Under Effect's @stability unstable policy, that makes this a minor release change.

For a separate discussion on splitting SemVer itself into Release | Prerelease so the else branch narrows too, see #11050.

主要言語
TypeScript
スター
16.7k
フォーク
808
平均マージ
21時間 22分
マージ済み PR(30日)
486

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Effect-TS/effect のほかの issue

Effect-TS/effect の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。