microsoft / microsoft/TypeScript
Assertion signature on generics doesn't narrow
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
Assertion AND signature
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Assertion Signatures, Generics.
⏯ Playground Link
💻 Code
// "Good assert": makes destructuring succeed
// function assert(c: unknown): asserts c {
// if (!c) {
// throw new TypeError('Assertion failed');
// }
// }
// "Bad assert": destructuring error
function assert<C>(c: C): asserts c {
if (!c) {
throw new TypeError('Assertion failed');
}
}
export function test(lines: string[]): string[] {
let func: {
name: string;
lines: string[];
} | null = null;
for (const line of lines) {
if (Math.random() < 0.5) {
func = {
name: "qwer",
lines: [line],
};
} else {
assert(func);
const {name, lines} = func; // <=== ERROR HERE: 'name' implicitly has type 'any'
lines.push(line);
assert(name !== 'abc');
}
}
if (func)
return func.lines;
return lines;
}
🙁 Actual behavior
When the generic-assert implementation is active, the line -
const {name, lines} = func;
errors with 'name' implicitly has type 'any'.
When the unknown assert implementation is active, types are properly inferred.
🙂 Expected behavior
Both assert versions contain assertion signature, and both should be able to narrow the type.
Additional information about the issue
No response
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere den Narrowing-Fehler aus dem verlinkten TypeScript Playground und vergleiche dabei die generische Assertion mit der Assertion mit einem unknown-Parameter. Verfolge die Verarbeitung von Assertion-Signaturen und das generische Control-Flow-Narrowing im Compiler und füge einen Regressionstest hinzu, der zeigt, dass Destructuring nach assert(func) name und lines ohne einen implicit-any-Fehler inferiert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100