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

Preserve literal types in contextual unions #19966

Merged
merged 14 commits into from
Dec 11, 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
90 changes: 47 additions & 43 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2786,7 +2786,7 @@ namespace ts {
/* @internal */ getNullType(): Type;
/* @internal */ getESSymbolType(): Type;
/* @internal */ getNeverType(): Type;
/* @internal */ getUnionType(types: Type[], subtypeReduction?: boolean): Type;
/* @internal */ getUnionType(types: Type[], subtypeReduction?: UnionReduction): Type;
/* @internal */ createArrayType(elementType: Type): Type;
/* @internal */ createPromiseType(type: Type): Type;

Expand Down Expand Up @@ -2841,6 +2841,13 @@ namespace ts {
/* @internal */ getAccessibleSymbolChain(symbol: Symbol, enclosingDeclaration: Node | undefined, meaning: SymbolFlags, useOnlyExternalAliasing: boolean): Symbol[] | undefined;
}

/* @internal */
export const enum UnionReduction {
None = 0,
Literal,
Subtype
}

export enum NodeBuilderFlags {
None = 0,
// Options
Expand Down
8 changes: 4 additions & 4 deletions src/services/codefixes/inferFromUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ namespace ts.codefix {
}
}
if (types.length) {
const type = checker.getWidenedType(checker.getUnionType(types, /*subtypeReduction*/ true));
const type = checker.getWidenedType(checker.getUnionType(types, UnionReduction.Subtype));
paramTypes[parameterIndex] = isRestParameter ? checker.createArrayType(type) : type;
}
}
Expand Down Expand Up @@ -542,12 +542,12 @@ namespace ts.codefix {
return checker.getStringType();
}
else if (usageContext.candidateTypes) {
return checker.getWidenedType(checker.getUnionType(map(usageContext.candidateTypes, t => checker.getBaseTypeOfLiteralType(t)), /*subtypeReduction*/ true));
return checker.getWidenedType(checker.getUnionType(map(usageContext.candidateTypes, t => checker.getBaseTypeOfLiteralType(t)), UnionReduction.Subtype));
}
else if (usageContext.properties && hasCallContext(usageContext.properties.get("then" as __String))) {
const paramType = getParameterTypeFromCallContexts(0, usageContext.properties.get("then" as __String).callContexts, /*isRestParameter*/ false, checker);
const types = paramType.getCallSignatures().map(c => c.getReturnType());
return checker.createPromiseType(types.length ? checker.getUnionType(types, /*subtypeReduction*/ true) : checker.getAnyType());
return checker.createPromiseType(types.length ? checker.getUnionType(types, UnionReduction.Subtype) : checker.getAnyType());
}
else if (usageContext.properties && hasCallContext(usageContext.properties.get("push" as __String))) {
return checker.createArrayType(getParameterTypeFromCallContexts(0, usageContext.properties.get("push" as __String).callContexts, /*isRestParameter*/ false, checker));
Expand Down Expand Up @@ -610,7 +610,7 @@ namespace ts.codefix {
}

if (types.length) {
const type = checker.getWidenedType(checker.getUnionType(types, /*subtypeReduction*/ true));
const type = checker.getWidenedType(checker.getUnionType(types, UnionReduction.Subtype));
return isRestParameter ? checker.createArrayType(type) : type;
}
return undefined;
Expand Down
136 changes: 136 additions & 0 deletions tests/baselines/reference/contextualTypeShouldBeLiteral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//// [contextualTypeShouldBeLiteral.ts]
interface X {
type: 'x';
value: string;
method(): void;
}

interface Y {
type: 'y';
value: 'none' | 'done';
method(): void;
}

function foo(bar: X | Y) { }

foo({
type: 'y',
value: 'done',
method() {
this;
this.type;
this.value;
}
});

interface X2 {
type1: 'x';
value: string;
method(): void;
}

interface Y2 {
type2: 'y';
value: 'none' | 'done';
method(): void;
}

function foo2(bar: X2 | Y2) { }

foo2({
type2: 'y',
value: 'done',
method() {
this;
this.value;
}
});

interface X3 {
type: 'x';
value: 1 | 2 | 3;
xtra: number;
}

interface Y3 {
type: 'y';
value: 11 | 12 | 13;
ytra: number;
}

let xy: X3 | Y3 = {
type: 'y',
value: 11,
ytra: 12
};

xy;


interface LikeA {
x: 'x';
y: 'y';
value: string;
method(): void;
}

interface LikeB {
x: 'xx';
y: 'yy';
value: number;
method(): void;
}

let xyz: LikeA | LikeB = {
x: 'x',
y: 'y',
value: "foo",
method() {
this;
this.x;
this.y;
this.value;
}
};

xyz;

//// [contextualTypeShouldBeLiteral.js]
"use strict";
function foo(bar) { }
foo({
type: 'y',
value: 'done',
method: function () {
this;
this.type;
this.value;
}
});
function foo2(bar) { }
foo2({
type2: 'y',
value: 'done',
method: function () {
this;
this.value;
}
});
var xy = {
type: 'y',
value: 11,
ytra: 12
};
xy;
var xyz = {
x: 'x',
y: 'y',
value: "foo",
method: function () {
this;
this.x;
this.y;
this.value;
}
};
xyz;
Loading