Skip to content

Regression in BCT when type uses 'this' in a member initializer during type inference #641

@RyanCavanaugh

Description

@RyanCavanaugh
interface PromiseHolder {
    promise: Promise<any>;
}

class Promise<T> {
    constructor(elem: T) { }
}

class Alpha implements PromiseHolder {
    promise: Promise<PromiseHolder>;
}

class Beta implements PromiseHolder {
    // Must use 'this' in the initializer here
    // Cannot have Promise<PromiseHolder> type annotation
    promise = new Promise<PromiseHolder>(this);
    extra: string;
}

var arr: PromiseHolder[];
var a = new Alpha(), b = new Beta();
arr = [a, b]; // Error in new compiler only: cannot convert {} to PromiseHolder

// Sanity checks
a = b; // OK, Beta is subtype of Alpha
arr[0] = a; // OK, Alpha is subtype of PromiseHolder
arr[0] = b; // OK, Beta is subtype of PromiseHolder
a = arr[0]; // OK, PromiseHolder and Alpha are equivalent under 'any'

Was not an error in TypeScript 1.0; is an error now. Appears specific to the use of this in the field initializer in Beta -- this shouldn't change the type inference (due to the explicit generic type parameter), nor should adding a redundant type annotation on the field. Found in internal partner codebase.

Metadata

Metadata

Assignees

Labels

Breaking ChangeWould introduce errors in existing codeBy DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions