Skip to content

Check base type for special property declarations #23671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4409,7 +4409,7 @@ namespace ts {
const special = getSpecialPropertyAssignmentKind(expression);
if (special === SpecialPropertyAssignmentKind.ThisProperty) {
const thisContainer = getThisContainer(expression, /*includeArrowFunctions*/ false);
// Properties defined in a constructor (or javascript constructor function) don't get undefined added.
// Properties defined in a constructor (or base constructor, or javascript constructor function) don't get undefined added.
// Function expressions that are assigned to the prototype count as methods.
declarationInConstructor = thisContainer.kind === SyntaxKind.Constructor ||
thisContainer.kind === SyntaxKind.FunctionDeclaration ||
Expand Down Expand Up @@ -4479,7 +4479,14 @@ namespace ts {
}
let type = jsDocType;
if (!type) {
// use only the constructor types unless only null | undefined (including widening variants) were assigned there
// use only the constructor types unless they were only assigned null | undefined (including widening variants)
if (definedInMethod) {
const propType = getTypeOfSpecialPropertyOfBaseType(symbol);
if (propType) {
(constructorTypes || (constructorTypes = [])).push(propType);
definedInConstructor = true;
}
}
const sourceTypes = some(constructorTypes, t => !!(t.flags & ~(TypeFlags.Nullable | TypeFlags.ContainsWideningType))) ? constructorTypes : types;
type = getUnionType(sourceTypes, UnionReduction.Subtype);
}
Expand All @@ -4493,6 +4500,20 @@ namespace ts {
return widened;
}

/** check for definition in base class if any declaration is in a class */
function getTypeOfSpecialPropertyOfBaseType(specialProperty: Symbol) {
const parentDeclaration = forEach(specialProperty.declarations, d => {
const parent = getThisContainer(d, /*includeArrowFunctions*/ false).parent;
return isClassLike(parent) && parent;
});
if (parentDeclaration) {
const classType = getDeclaredTypeOfSymbol(getSymbolOfNode(parentDeclaration)) as InterfaceType;
const baseClassType = classType && getBaseTypes(classType)[0];
if (baseClassType) {
return getTypeOfPropertyOfType(baseClassType, specialProperty.escapedName);
}
}
}

// Return the type implied by a binding pattern element. This is the type of the initializer of the element if
// one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/salsa/a.js ===
class Base {
>Base : Symbol(Base, Decl(a.js, 0, 0))

constructor() {
this.p = 1
>this.p : Symbol(Base.p, Decl(a.js, 1, 19))
>this : Symbol(Base, Decl(a.js, 0, 0))
>p : Symbol(Base.p, Decl(a.js, 1, 19))
}
}
class Derived extends Base {
>Derived : Symbol(Derived, Decl(a.js, 4, 1))
>Base : Symbol(Base, Decl(a.js, 0, 0))

m() {
>m : Symbol(Derived.m, Decl(a.js, 5, 28))

this.p = 1
>this.p : Symbol(Derived.p, Decl(a.js, 6, 9))
>this : Symbol(Derived, Decl(a.js, 4, 1))
>p : Symbol(Derived.p, Decl(a.js, 6, 9))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/salsa/a.js ===
class Base {
>Base : Base

constructor() {
this.p = 1
>this.p = 1 : 1
>this.p : number
>this : this
>p : number
>1 : 1
}
}
class Derived extends Base {
>Derived : Derived
>Base : Base

m() {
>m : () => void

this.p = 1
>this.p = 1 : 1
>this.p : number
>this : this
>p : number
>1 : 1
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/conformance/salsa/a.js ===
class Base {
>Base : Symbol(Base, Decl(a.js, 0, 0))

m() {
>m : Symbol(Base.m, Decl(a.js, 0, 12))

this.p = 1
>this.p : Symbol(Base.p, Decl(a.js, 1, 9))
>this : Symbol(Base, Decl(a.js, 0, 0))
>p : Symbol(Base.p, Decl(a.js, 1, 9))
}
}
class Derived extends Base {
>Derived : Symbol(Derived, Decl(a.js, 4, 1))
>Base : Symbol(Base, Decl(a.js, 0, 0))

m() {
>m : Symbol(Derived.m, Decl(a.js, 5, 28))

// should be OK, and p should have type number | undefined from its base
this.p = 1
>this.p : Symbol(Derived.p, Decl(a.js, 6, 9))
>this : Symbol(Derived, Decl(a.js, 4, 1))
>p : Symbol(Derived.p, Decl(a.js, 6, 9))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/salsa/a.js ===
class Base {
>Base : Base

m() {
>m : () => void

this.p = 1
>this.p = 1 : 1
>this.p : number | undefined
>this : this
>p : number | undefined
>1 : 1
}
}
class Derived extends Base {
>Derived : Derived
>Base : Base

m() {
>m : () => void

// should be OK, and p should have type number | undefined from its base
this.p = 1
>this.p = 1 : 1
>this.p : number | undefined
>this : this
>p : number | undefined
>1 : 1
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/conformance/salsa/a.js ===
class Base {
>Base : Symbol(Base, Decl(a.js, 0, 0))

m() {
>m : Symbol(Base.m, Decl(a.js, 0, 12))

this.p = 1
>this.p : Symbol(Base.p, Decl(a.js, 1, 9))
>this : Symbol(Base, Decl(a.js, 0, 0))
>p : Symbol(Base.p, Decl(a.js, 1, 9))
}
}
class Derived extends Base {
>Derived : Symbol(Derived, Decl(a.js, 4, 1))
>Base : Symbol(Base, Decl(a.js, 0, 0))

constructor() {
super();
>super : Symbol(Base, Decl(a.js, 0, 0))

// should be OK, and p should have type number from this assignment
this.p = 1
>this.p : Symbol(Derived.p, Decl(a.js, 7, 16))
>this : Symbol(Derived, Decl(a.js, 4, 1))
>p : Symbol(Derived.p, Decl(a.js, 7, 16))
}
test() {
>test : Symbol(Derived.test, Decl(a.js, 10, 5))

return this.p
>this.p : Symbol(Derived.p, Decl(a.js, 7, 16))
>this : Symbol(Derived, Decl(a.js, 4, 1))
>p : Symbol(Derived.p, Decl(a.js, 7, 16))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/conformance/salsa/a.js ===
class Base {
>Base : Base

m() {
>m : () => void

this.p = 1
>this.p = 1 : 1
>this.p : number | undefined
>this : this
>p : number | undefined
>1 : 1
}
}
class Derived extends Base {
>Derived : Derived
>Base : Base

constructor() {
super();
>super() : void
>super : typeof Base

// should be OK, and p should have type number from this assignment
this.p = 1
>this.p = 1 : 1
>this.p : number
>this : this
>p : number
>1 : 1
}
test() {
>test : () => number

return this.p
>this.p : number
>this : this
>p : number
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @noImplicitAny: true
// @strictNullChecks: true
// @Filename: a.js
class Base {
constructor() {
this.p = 1
}
}
class Derived extends Base {
m() {
this.p = 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @noImplicitAny: true
// @strictNullChecks: true
// @Filename: a.js
class Base {
m() {
this.p = 1
}
}
class Derived extends Base {
m() {
// should be OK, and p should have type number | undefined from its base
this.p = 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @noImplicitAny: true
// @strictNullChecks: true
// @Filename: a.js
class Base {
m() {
this.p = 1
}
}
class Derived extends Base {
constructor() {
super();
// should be OK, and p should have type number from this assignment
this.p = 1
}
test() {
return this.p
}
}