microsoft / microsoft/TypeScript

Assertion signature on generics doesn't narrow

Aperta
#60,130 13 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Control Flow Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 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

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&allowUnusedLabels=true&downlevelIteration=true&noEmitHelpers=true&noUnusedLocals=true&noUnusedParameters=true&preserveConstEnums=true&removeComments=true&importHelpers=true&target=99&useUnknownInCatchVariables=false&exactOptionalPropertyTypes=true&noImplicitOverride=true&noFallthroughCasesInSwitch=true&noPropertyAccessFromIndexSignature=true&inlineSourceMap=true&inlineSources=true&stripInternal=true&ts=5.6.2#code/FAehAICIHEHtYCbgIYGdUFMBOAXSAucAW2QGsNVwEKcsBXAYxzqwEsA7Ac3FUYYwwJQEAGZ12TVrHYp02HAAoGhcaXawA7uwCUhNJlyUG4AN7DwFi6xHgFAQgbbT5y6-A4AFlk3h2GDeAAKgCeAA4YAKJY3lgKAOQAgnK4UjIiyKwANoJx2gDcLpYAvuYl5pAAQshI+vIEVDT0TCwc3NgxwGISOKmyBjgAPADCAHxKhEO6ffJGzq7Wtg5OZm6Wnt4BfgEh4VEx8Un9velZOfnAriVlGAAeobC44F2S0u40CpkcFISotK0A2gBdKa-NhcIHOC6rcDZHBPcTKObQ1zsZBEDA-P5cArI1yfPyoTFgzhAnHQorgAA+vjomUy4AAvDS6QUoasRA9bAxpL8YV9wLAbPiKMs2biFgoALLITwAOiwyHYCFgRAUTgG4AADLKAKyi3HQ56MpEG5Go9GESAARw02EgABoxaa3MLCeB-sLAY7nZcyQaKRhMpgTT7argFM9zj7LNz2LyTOaMPa+QSKUznnlLGBwAMGXnwBEAEqFgDyhfAAAkixEnabXbLQnRUB4Pl8o9HpuHE+A7Pm4sgAEYMXJ+3ElZHj6ESyO11ZYDDMLBpBGy12j1zzxcyNdiopAA

💻 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il fallimento del narrowing dal TypeScript Playground collegato, confrontando l’asserzione generica con l’asserzione con un parametro unknown. Traccia la gestione delle firme di asserzione e del narrowing generico del flusso di controllo nel compilatore, quindi aggiungi un test di regressione che mostri che il destructuring dopo assert(func) inferisce name e lines senza un errore di implicit-any.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.