Skip to content

how do i prevent non-nullable types in arguments #22375

Closed
@zpdDG4gta8XKpMCd

Description

so we have the following convenience function:

export function asDefinedOr<T, D>(value: T | undefined, defaultValue: D): T | D {
    return value !== undefined ? value : defaultValue;
}

we would like to prevent the following from happening

declare var x : number;
asDefinedOr(x, 0); // <-- problem, unnecessary call, because x cannot be undefined

so we would like to make only T | undefined types allowed as aguments but not just T

is there a way to do it using conditional types?

Metadata

Assignees

No one assigned

    Labels

    QuestionAn 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