Content-Length: 313677 | pFad | https://github.com/microsoft/TypeScript/issues/18466

80 Typescript type checks passing a callback with non-optional parameter for optional one, but throws error at runtime · Issue #18466 · microsoft/TypeScript · GitHub
Skip to content

Typescript type checks passing a callback with non-optional parameter for optional one, but throws error at runtime #18466

Closed
@afroozeh

Description

@afroozeh

TypeScript Version: 2.4.1

Code

function f(callback: (a: string, b?: string) => void) {
    callback("a", undefined);
}

f((a: string, b: string) => {
    console.log(a.length, b.length);
});

Expected behavior:
Compile error with when compiled with the strict flag.
(a: string, b: string) => void is not a subtype of (a: string, b?: string) => void as string is not a supertype of string | undefined, I expected a compile error.

Actual behavior:
Compiles, but throws error at runtime

console.log(a.length, b.length);
                       ^

TypeError: Cannot read property 'length' of undefined
    at /Users/Ali/test.js:11:28
    at f (/Users/Ali/test.js:3:5)
    at Object.<anonymous> (/Users/Ali/test.js:10:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueQuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/microsoft/TypeScript/issues/18466

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy