Skip to content
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

Fix nullable 'this' type #14510

Merged
merged 3 commits into from
Mar 7, 2017
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
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11601,7 +11601,7 @@ namespace ts {
}
}
}
if (compilerOptions.noImplicitThis) {
if (noImplicitThis) {
const containingLiteral = getContainingObjectLiteral(func);
if (containingLiteral) {
// We have an object literal method. Check if the containing object literal has a contextual type
Expand All @@ -11622,9 +11622,9 @@ namespace ts {
type = getApparentTypeOfContextualType(literal);
}
// There was no contextual ThisType<T> for the containing object literal, so the contextual type
// for 'this' is the contextual type for the containing object literal or the type of the object
// literal itself.
return contextualType || checkExpressionCached(containingLiteral);
// for 'this' is the non-null form of the contextual type for the containing object literal or
// the type of the object literal itself.
return contextualType ? getNonNullableType(contextualType) : checkExpressionCached(containingLiteral);
}
// In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
// contextual type for 'this' is 'obj'.
Expand Down
99 changes: 99 additions & 0 deletions tests/baselines/reference/thisTypeInObjectLiterals2.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ let p1: Point = {
}
};

let p2: Point | null = {
x: 10,
y: 20,
moveBy(dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
};

let p3: Point | undefined = {
x: 10,
y: 20,
moveBy(dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
};

let p4: Point | null | undefined = {
x: 10,
y: 20,
moveBy(dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
};

declare function f1(p: Point): void;

f1({
Expand All @@ -61,6 +97,20 @@ f1({
}
});

declare function f2(p: Point | null | undefined): void;

f2({
x: 10,
y: 20,
moveBy(dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
});

// In methods of an object literal with a contextual type that includes some
// ThisType<T>, 'this' is of type T.

Expand Down Expand Up @@ -196,6 +246,7 @@ vue.hello;
//// [thisTypeInObjectLiterals2.js]
// In methods of an object literal with no contextual type, 'this' has the type
// of the object literal.
"use strict";
var obj1 = {
a: 1,
f: function () {
Expand Down Expand Up @@ -228,6 +279,39 @@ var p1 = {
}
}
};
var p2 = {
x: 10,
y: 20,
moveBy: function (dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
};
var p3 = {
x: 10,
y: 20,
moveBy: function (dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
};
var p4 = {
x: 10,
y: 20,
moveBy: function (dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
};
f1({
x: 10,
y: 20,
Expand All @@ -239,6 +323,17 @@ f1({
}
}
});
f2({
x: 10,
y: 20,
moveBy: function (dx, dy, dz) {
this.x += dx;
this.y += dy;
if (this.z && dz) {
this.z += dz;
}
}
});
var x1 = makeObject({
data: { x: 0, y: 0 },
methods: {
Expand Down Expand Up @@ -328,7 +423,11 @@ declare type Point = {
moveBy(dx: number, dy: number, dz?: number): void;
};
declare let p1: Point;
declare let p2: Point | null;
declare let p3: Point | undefined;
declare let p4: Point | null | undefined;
declare function f1(p: Point): void;
declare function f2(p: Point | null | undefined): void;
declare type ObjectDescriptor<D, M> = {
data?: D;
methods?: M & ThisType<D & M>;
Expand Down
Loading