Skip to content

How to define a parameter as optional, but if specified be ensured as not undefined. #11988

Closed
@electricessence

Description

@electricessence

This is a bit tough to explain but probably similar to: #10014

TypeScript Version: 2.0.6

Code

So if I want to specify (when using strict null checks):

export interface Selector<TSource, TResult>
{
	(source:TSource, index?:number):TResult;
}

Which will allow me to use functions that take 1 or 2 parameters where the second one is a number or undefined. But what I really want is that if a second parameter is defined it will be a number.

Any signature style overrides I tried seemed to just screw things up more.
Is there a workaround for this without having to create separate interfaces like:

export interface Selector<TSource, TResult>
{
	(source:TSource, index?:number):TResult;
}
export interface SelectorIndexed<TSource, TResult>
{
	(source:TSource, index:number):TResult;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions