Skip to content

Default values in combination with object destructuring are not type checked within function declarations. #19127

Closed
@bverbist

Description

@bverbist

TypeScript Version: 2.5.3

Code

// ===== string literal default value =====

// TS does not give an error for the default value 'z'
function defaultUsingStringLiteralWithinObject({someInput = 'z'}: {someInput?: 'x' | 'y'}) {
    console.log(someInput)
}

// but when calling the function this correctly gives TS2345 (Type 'z' is not assignable to type 'x' | 'y')
defaultUsingStringLiteralWithinObject({someInput: 'z'})

// p.s. TS correctly gives TS2322 when invalid default without object destructuring
function defaultUsingStringLiteral(someInput?: 'x' | 'y' = 'z') {
    console.log(someInput)
}

// ===== enum default value =====

enum IOtherInput {
    x = 'x',
    y = 'y'
}

// TS does not give an error for the default value 'z'
function defaultUsingEnumWithinObject({otherInput = 'z'}: {otherInput?: IOtherInput}) {
    console.log(otherInput)
}

// but when calling the function this correctly gives TS2345 (Type 'z' is not assignable to type 'IOtherInput')
defaultUsingEnumWithinObject({otherInput: 'z'})

// p.s. TS correctly gives TS2322 when invalid default without object destructuring
function defaultUsingEnum(otherInput?: IOtherInput = 'z') {
    console.log(otherInput)
}

Expected behavior:
Default values - in combination with object destructuring - should also be type checked in the function declaration.

Actual behavior:
Default values - in combination with object destructuring - are not type checked in the function declaration. Noticed this behaviour for both string literals and enums.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions