Skip to content

generics Instantiated as null : {} causes other type errors to disappear #3733

Closed
@bergmark

Description

@bergmark

Examples:

class C<R>
{
  value : R;
  constructor(v? : R) { this.value = v }
}

// OK, no type error
var a : C<string> = new C("x");

// OK, type error
var b : C<string> = new C(1);
var c : C<string> = true ? new C("x") : new C(1);
var d : C<string> = new C(1) || new C<string>(null);
function e() : C<string> { if (false) { return new C(null) } else { return new C(1) } }

// Bad, no type error
var f : C<string> = true ? new C(null) : new C(1);
var g : C<string> = true ? new C(1) : new C(null);
var h : C<string> = (() => { if (false) { return new C(null) } else { return new C(1) } })();
var i : C<string> = new C(1) || new C(null);

Here, any time new C(1) is used it should have the type C<number> and not unify with C<string>. It seems like R not being known in new C(null) prevents these other type errors from occurring.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions