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

Abierto
#11,048 0 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@effect-bot ya está trabajando en esto.

Desde el 29/11/2026.

  • #11049 de @jasonkuhrt — abierto

Evaluación

Este issue todavía no se ha evaluado.

Descripción

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.

Lenguaje dominante
TypeScript
Estrellas
16.7k
Forks
808
Merge medio
21 h 22 min
PR fusionados (30 d)
486

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Effect-TS/effect

Todos los issues de Effect-TS/effect

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.