microsoft / microsoft/TypeScript

strictNullCheck False Posivitve when access propery on created object.

Offen
#31,202 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Control Flow Experience Enhancement Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TypeScript Version: 3.5.0-dev.20190501

Search Terms:
strict false positive object

Code

import * as azure from "@azure/storage-blob";

interface ISaveBlobOptions {
    blobName: string;
    contentDisposition?: {
        fileName: string;
        type: "inline" | "attachment";
    };

    contentType: string;

    fileData: Buffer;
}

const saveBlobBase = async (input: ISaveBlobOptions): Promise<void> => {
    this.validateSaveBlobBase(input);

    await this.createStorage();

    const options: azure.IBlockBlobUploadOptions = {
        blobHTTPHeaders: {
            blobContentType: input.contentType
        }
    };

    if(input.contentDisposition) {
        // Getting Object is possibly 'undefined'.
        options.blobHTTPHeaders.blobContentDisposition = `${input.contentDisposition.type}; filename="${input.contentDisposition.fileName}"`;
    }

    if (input.fileData instanceof Buffer) {
        await this.saveBlobBuffer(input.blobName, input.fileData, options);
    } else if(this.isMulterFile(input.fileData)) {
        await this.saveBlobMulterFile(input.blobName, input.fileData, options);
    } else {
        throw new Error("Unknown type for param file");
    }
}

saveBlobBase({
    blobName: "myBlobName",
    contentDisposition: {
        fileName: "foo.bar",
        type: "inline"
    },
    contentType: "text/plain",
    fileData: Buffer.from("SOME TEXT")
});

tsconfig

{
	"compilerOptions": {
		"target": "es2017",
		"module": "commonjs",
		"sourceMap": true,
		"strictPropertyInitialization": true,
		"strictNullChecks": true
	}
}

Expected behavior:

since when the creating the options variable the property blobHTTPHeaders is also defined. I would expect that accessing properties off of blobHTTPHeaders should not throw a "Object is possibly 'undefined'."

Actual behavior:

Getting Object is possibly 'undefined'.

Playground Link:

Related Issues:

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die Diagnose mit dem Beispiel aus der TypeScript-3.5-Ära und seiner tsconfig zu reproduzieren, und vergleiche dann das Verhalten mit dem aktuellen Compiler. Untersuche, wie der type checker Eigenschaften eingrenzt, die in object literals initialisiert werden, und füge einen regression test hinzu, wenn sich bestätigt, dass das Verhalten inkorrekt ist. Als erledigt gilt die Aufgabe, wenn das Beispiel die false-positive-Diagnose nicht mehr meldet, ohne strictNullChecks abzuschwächen.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.