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

Đang mở
#11,048 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@effect-bot đang làm issue này rồi.

Từ ngày 29/11/2026.

  • #11049 của @jasonkuhrt — đang mở

Đánh giá

Issue này chưa được đánh giá.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
16.7k
Fork
808
Merge trung bình
21 giờ 22 phút
Pull request đã merge (30 ngày)
486

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Effect-TS/effect

Tất cả issue của Effect-TS/effect

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.