SC2003 'string | undefined' when a method returning an element of its array field is used inline
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 30/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Stack tecnológico
- typescript
- Área
- compilers
Línea de trabajo
Reproduce first: save the snippet as repro.ts and run scriptc build repro.ts; the rejection is emitted by the SC2003 union-conversion layout check. Locate where that diagnostic fires and how an inline method-call result reaches it differently than a const-bound value; the const and plain-function variants that already compile are the contrast. Read the #2319 fix for "string concatenation with possibly missing reads" as prior art. Done means the repro compiles and prints 2 with a regression test guarding it.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
A class method declared to return string returns an element of its own string[] field. Using the call result inline, as in obj.last().length, is rejected with SC2003 got 'string | undefined'. Binding the result to a const first compiles.
Reproduction
class Args {
list: string[] = ["a", "bc"];
last(): string {
return this.list[this.list.length - 1];
}
}
console.log(new Args().last().length);
$ node repro.ts
2
$ scriptc build repro.ts -o repro
repro.ts:7:13 - error SC2003: union conversion has no unambiguous layout mapping: expected 'string', got 'string | undefined' (each source arm needs an identical destination arm, a unique compatible record/array layout, or a shared literal discriminant selecting compatible layouts)
It fails 3 out of 3 builds.
What matters
| variant | result |
|---|---|
| repro as above | SC2003 |
const value = new Args().last(); console.log(value.length); |
compiles, prints 2 |
same body as a plain function last(list: string[]): string, used inline |
compiles, prints 2 |
plain function reading the array from a Map, with undefined/empty guards, used inline |
compiles |
So it needs both a method that returns an element read and an inline use of the call result. At the call site the declared return type is string, and there's no union in the source. The limitations page (array-bounds-and-runtime-errors) says a missing element that reaches storage that can't hold undefined throws a TypeError at run time. A build-time rejection that disappears once the value goes through a const looks inconsistent with that.
Possibly related
#2141 (fixed by #2319, after 0.4.5) has the same shape: an inline call result, a body that reads an array element, and binding to a variable avoids it. Its trigger was string concatenation ("x-" + PAD[i]), and the fix describes "string concatenation with possibly missing reads". This repro has no concatenation, and no release after #2319 exists yet, so I couldn't check whether it's covered.
Versions
- scriptc 0.0.36: compiles and prints
2. - 0.1.1, the next version published to npm: SC2003.
- 0.4.5: still SC2003.
SCRIPTC_CHECKER=tsgogives the same result.
Where it came from
A CLI argument parser, args.value("as", "").length > 0. value() returns the last element of a string[] or a fallback. This compiled with 0.0.36 and blocks upgrading to 0.4.5.
Environment
- scriptc 0.4.5 (
@scriptc/cli-darwin-arm64) - macOS 26 arm64
- Node v24.16.0 as the reference
- Lenguaje dominante
- TypeScript
- Estrellas
- 6.4k
- Forks
- 201
- Merge medio
- 6 h 56 min
- PR fusionados (30 d)
- 916
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de vercel-labs/scriptc
-
util.format %o passes the wrong showHidden flag to custom inspectorsPosiblemente ocupada @MathurAditya724 la tomó hace 1 día. Abierto
Dificultad 4/5 1-2 días Aptitud para principiantes 35/100
vercel-labs/scriptc#2571 ·
Los mantenedores suelen responder en 1 día
-
process.cwd.name keeps the main-thread name inside WorkersPosiblemente ocupada @MathurAditya724 la tomó hace 1 día. Abierto
Dificultad 4/5 1-2 días Aptitud para principiantes 35/100
vercel-labs/scriptc#2557 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 3-5 días Aptitud para principiantes 30/100
vercel-labs/scriptc#2399 ·
Los mantenedores suelen responder en 1 día
-
Large LLVM metadata tables crash compilation with a stack overflowPosiblemente ocupada @MathurAditya724 la tomó hace 3 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 22/100
vercel-labs/scriptc#2394 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 20/100
vercel-labs/scriptc#2209 ·
Los mantenedores suelen responder en 1 día
Todos los issues de vercel-labs/scriptc
Issues similares
-
Replace discontinued crypto-js in the themePosiblemente ocupada @aelmanaa la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
PaloAltoNetworks/docusaurus-openapi-docs#1689 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
solana-foundation/solana-web3.js#4015 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
formatSearchQuery drops the :present modifier, so `email:present=true` is sent as `email=true`Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día
-
documentation
Dificultad 1/5 1-3 horas Aptitud para principiantes 90/100
ivopogace/opencode-courier#422 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
chapel-lang/chapel-vscode#158 ·
Los mantenedores suelen responder en 1 día