microsoft / microsoft/TypeScript

strictNullCheck False Posivitve when access propery on created object.

Open
#31,202 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: check: Control Flow Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the diagnostic with the TypeScript 3.5-era example and its tsconfig, then compare the behavior with the current compiler. Investigate how the type checker narrows properties initialized in object literals, and add a regression test if the behavior is confirmed as incorrect. Done means the example no longer reports the false-positive diagnostic without weakening strictNullChecks.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.