Does TypeScript allow default parameter values in returned functions

4 days ago 8
ARTICLE AD BOX

Consider the following code:

type TParser = (str: string, errStr: string) => number; function LIT(str: string): TParser { return function (input: string, errStr="An error occurred"): number { if (input.startsWith(str)) { return str.length; } else { console.log(errStr); return -1; } } } let func = LIT('abc') let result = func('abc')

The function returned from LIT('abc') is a function that should take either 1 or 2 arguments because a default value was provided for the second parameter. However, when I run this script, I get this error:

TS2554 [ERROR]: Expected 2 arguments, but got 1. let result = func('abc') ~~~~ at file:///C:/Users/johnd/hera/source/bug.ts:17:14 An argument for 'errStr' was not provided. type TParser = (str: string, errStr: string) => number; ~~~~~~~~~~~~~~ at file:///C:/Users/johnd/hera/source/bug.ts:1:30 error: Type checking failed.

Am I doing something wrong or is this a TypeScript bug? In my opinion, if I'm not allowed to provide default values for a function returned from another function, TypeScript should at least warn me that it's invalid TypeScript.

In case it's relevant, I'm using deno (not nodejs). Here is version info, though I believe that deno just defers to TypeScript, so it's the TypeScript version that's relevant:

$ deno --version deno 2.5.6 (stable, release, x86_64-pc-windows-msvc) v8 14.0.365.5-rusty typescript 5.9.2
Read Entire Article