Skip to content

optional parameter for generics is being ignoredΒ #43251

@plusgut

Description

@plusgut

Bug Report

πŸ”Ž Search Terms

generics, optional parameter, default generic type, generic constraint

πŸ•— Version & Regression Information

  • This changed between versions 4.1.5 and 4.2.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface SomeInterface<T> {
    value: T
}

declare function factory<T extends string = string>(optionalParameter?: T): SomeInterface<T>;
declare function echo<K extends {[key: string]: SomeInterface<any>}>(opt: K ): K;

const fallbackToConstraintInsteadOfDefault = echo({ foo: factory() } as const);

πŸ™ Actual behavior

the return type of echo is not fully of type K, but falls back to the constraint of K.

πŸ™‚ Expected behavior

The return type is from the default type of T.

Notes

This behaviour does not occur in 4.1.5
This behaviour does not occur when no constraint for K is given.
This behaviour does not occur when factory gets called with a parameter.
This behaviour does not occur when the return value of factory is first stored in a variable.

Metadata

Metadata

Assignees

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