Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS2532: Object is possibly undefined #49121

Closed
Dirain1700 opened this issue May 16, 2022 · 3 comments
Closed

TS2532: Object is possibly undefined #49121

Dirain1700 opened this issue May 16, 2022 · 3 comments

Comments

@Dirain1700
Copy link

Dirain1700 commented May 16, 2022

Bug Report

πŸ”Ž Search Terms

TS2532, possibly undefined

πŸ•— Version & Regression Information

typescript@4.6.2
@types/node@17.0.33

Node.js v16.14.2
npm v8.1.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

//@target: esnext
//@moduleResolution: node
//@module: commonjs
declare var process: any;
declare var Console: any;
(() => {
    const arg: string = process.argv[2]!;
    const arr: string[] = process.argv;
    let info: string | undefined = undefined;

    info = arg;

    if (info === undefined) {
        console.log([]);
        return;
    }

    console.log(arr.find((e) => e === info));// No error
    console.log(arr.find((e) => e.length === info.length));// Error: TS2532: Object is possibly undefined
    console.log(info);// No error (This is correct)
    console.log(info.length);// No error
})();
Output
"use strict";
(() => {
    const arg = process.argv[2];
    const arr = process.argv;
    let info = undefined;
    info = arg;
    if (info === undefined) {
        console.log([]);
        return;
    }
    console.log(arr.find((e) => e === info)); // No error
    console.log(arr.find((e) => e.length === info.length)); // Error: TS2532: Object is possibly undefined
    console.log(info); // No error (This is correct)
    console.log(info.length); // No error
})();
Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "Latest",
    "module": "CommonJS",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

πŸ™ Actual behavior

Unable to compile TypeScript and compiler says "'info' is undefied".
if my code is wrong, I should get same error at line 18 and 21, but I didn't get that error.

πŸ™‚ Expected behavior

"info" is not undefined because already returned, so it should be compiled.

I don't know that cause.

@Josh-Cena
Copy link
Contributor

Josh-Cena commented May 16, 2022

Duplicate of #9998?

Narrowing is not preserved across callback boundaries. There's even a template designed for it which will get your issue auto-closedπŸ˜†

@Dirain1700
Copy link
Author

Should I add a note to that Issue?

@Josh-Cena
Copy link
Contributor

No, there's no need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants