Ciro Santilli OurBigBook.com  Sponsor 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
typescript/functionOptionalArgument.ts
// https://cirosantilli.com/typescript

const assert = require('assert')

function myfunc(
  myInt: number,
  // We can us the `?` here directly to indicate that it is optional.
  myString?: string,
) {
  return `${myInt} ${myString}`
}
assert.strictEqual(myfunc(1, 'abc'), '1 abc')
assert.strictEqual(myfunc(1), '1 undefined')
// Type error.
//assert.strictEqual(myfunc(1, 1), '1 1')

function myfunc2(
  myInt: number,
  // This achieves a similar effect. But then I don't know how to set the type of myString.
  // so it is less good.
  myString=undefined,
) {
  return `${myInt} ${myString}`
}

assert.strictEqual(myfunc2(1, 'abc'), '1 abc')
assert.strictEqual(myfunc2(1), '1 undefined')
// Type not enforced. Bad.
assert.strictEqual(myfunc2(1, 1), '1 1')