Skip to content

StrictNullChck with Object.assign anomaly (Object is possibly 'undefined') #43110

Closed
@holdfenytolvaj

Description

@holdfenytolvaj

Bug Report

With the strict null check turned on

function test(options?: { fields?: string , strict?: boolean}) {
    options = Object.assign({ fields: null }, options);
    if (options.fields) {
       //^^^^^^^ Object is possibly 'undefined'.ts(2532)
    }
    const test2 = () => {
            if (options.strict) {
               //^^^^^^^ Object is possibly 'undefined'.ts(2532)
            }
    }
}

Any of the following changes fix the first (but not the second):

function test(options?: { fields?: string|null }) {
    options = Object.assign({ fields: '' }, options);
    options = Object.assign({ a:null }, options);

🔎 Search Terms

strictNullCheck, Object.assign

🕗 Version & Regression Information

4.1.3

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABFApgZygCjgB1gtAfgC5EBvRYGFAGwBMjSMAnGMAcwBpEWZoTEAIzhwaKAIZIAvgEpyAKERLEufGDSIAvIgDyggFYpoAOnFo0MdmEwUqtBqTAgaNRFO6r46mQG5FymGBEbDwvNGM7ejQ5Mn9lZQB6BIA9VLTk3QMjKEQYDRw4cxhBGgBPRABycDoUKjAUOgrjKDRMACYAVgBmNpk4t36IAhzUDDatYLlNAD4FePmAoJC1cN5oGP6FhaT0tMzDaFz8wosS8qqwGrqGppb27t7Nral+l6kgA

💻 Code

🙁 Actual behavior

Wrongly assumes the options can be undefined

🙂 Expected behavior

Should be no error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions