Skip to content

Weird type inference / defaulting behavior #11112

@Ptival

Description

@Ptival

TypeScript Version: nightly (2.1.0-dev.20160915)

Code

declare class Maybe<T> {
  static nothing<T>(): Maybe<T>;
//   valueOrThrow(error?: Error): T;
}

function muchAdoAbout(m: Maybe<string>): void {}
muchAdoAbout(Maybe.nothing())

Expected behavior:
When the commented out line is uncommented, the code should still type-check (the nothing on the last line should get its type found to be string by use-site inference like it does already).

Actual behavior:
When you uncomment the valueOrThrow line, the type inferred at the call site becomes {} and the code does not type-check anymore.

I'm not sure why adding that line has this effect. Note that it works if you do instead:

//   valueOrThrow<T>(error?: Error): T;

But I'd like to understand if the behavior shown here is a bug or intended...

Not sure if related to:
#5254

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions