Discriminated union diff

Created Diff never expires
17 削除
合計
削除
単語
合計
削除
この機能を引き続き使用するには、アップグレードしてください
Diffchecker logo
Diffchecker Pro
29
17 追加
合計
追加
単語
合計
追加
この機能を引き続き使用するには、アップグレードしてください
Diffchecker logo
Diffchecker Pro
29
type Options = { foo: 'bar' }
type Common = 'c'


type ArgsRegExp = [first: RegExp, second?: Options]
type ArgsRegExp = [first: 'a0', second?: Common]
type ArgsString = [first: string, second?: string, third?: Options]
type ArgsString = [first: 'b0', second?: 'b1', third?: Common]
type Args = ArgsRegExp | ArgsString
type Args = ArgsRegExp | ArgsString


interface Params {
interface Params {
readonly pattern: RegExp
readonly custom: string
readonly options?: Options
readonly common?: Common
}
}


function getParams(args: ArgsRegExp): Params
function getParams(args: ArgsRegExp): Params
function getParams(args: ArgsString): Params
function getParams(args: ArgsString): Params
function getParams([first, second, third]: Args): Params {
function getParams([first, second, third]: Args): Params {
if (first instanceof RegExp) {
if (first === 'a0') {
return {
return {
pattern: first,
custom: first,
options: second,
common: second,
}
}
}
}


return {
return {
pattern: new RegExp(
custom:
first,
first +
second
second,
),
common: third,
options: third,
}
}
}
}