strictNullCheck False Posivitve when access propery on created object.

未關閉
#31,202 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
typescript
領域
compilers

研究方向

先使用 TypeScript 3.5 時代的範例及其 tsconfig 重現該診斷,然後將其行為與目前的 compiler 進行比較。調查 type checker 如何縮小在 object literals 中初始化的屬性;如果確認該行為不正確,則新增 regression test。完成的標準是:在不削弱 strictNullChecks 的情況下,該範例不再回報 false-positive 診斷。

由索引模型根據 Issue 內容生成。

描述

Domain: check: Control Flow Experience Enhancement Suggestion

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:

主要語言
Go
星號
111k
分支
14.4k
平均合併
1 天 19 小時
30 天內合併 PR
117

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。