Skip to content

Non-widening literal type inferences #24310

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 5 commits into from
May 22, 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
14 changes: 9 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9286,8 +9286,10 @@ namespace ts {
return type;
}

function getRegularTypeOfLiteralType(type: Type) {
return type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral ? (<LiteralType>type).regularType : type;
function getRegularTypeOfLiteralType(type: Type): Type {
return type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral ? (<LiteralType>type).regularType :
type.flags & TypeFlags.Union ? getUnionType(sameMap((<UnionType>type).types, getRegularTypeOfLiteralType)) :
type;
}

function getLiteralType(value: string | number, enumId?: number, symbol?: Symbol) {
Expand Down Expand Up @@ -12774,10 +12776,12 @@ namespace ts {
// all inferences were made to top-level occurrences of the type parameter, and
// the type parameter has no constraint or its constraint includes no primitive or literal types, and
// the type parameter was fixed during inference or does not occur at top-level in the return type.
const widenLiteralTypes = inference.topLevel &&
!hasPrimitiveConstraint(inference.typeParameter) &&
const primitiveConstraint = hasPrimitiveConstraint(inference.typeParameter);
const widenLiteralTypes = !primitiveConstraint && inference.topLevel &&
(inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter));
const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : candidates;
const baseCandidates = primitiveConstraint ? sameMap(candidates, getRegularTypeOfLiteralType) :
widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) :
candidates;
// If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if
// union types were requested or if all inferences were made from the return type position, infer a
// union type. Otherwise, infer a common supertype.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var derived2: Derived2;

var r2 = true ? 1 : '';
>r2 : string | number
>true ? 1 : '' : 1 | ""
>true ? 1 : '' : "" | 1
>true : true
>1 : 1
>'' : ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var i: I<string>;
>I : I<T>

var y = i(""); // y should be string
>y : string
>y : ""
>i("") : ""
>i : I<string>
>"" : ""
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/conditionalExpression1.types
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
=== tests/cases/compiler/conditionalExpression1.ts ===
var x: boolean = (true ? 1 : ""); // should be an error
>x : boolean
>(true ? 1 : "") : 1 | ""
>true ? 1 : "" : 1 | ""
>(true ? 1 : "") : "" | 1
>true ? 1 : "" : "" | 1
>true : true
>1 : 1
>"" : ""
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/conditionalExpressions2.types
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var b = false ? undefined : 0;

var c = false ? 1 : 0;
>c : number
>false ? 1 : 0 : 1 | 0
>false ? 1 : 0 : 0 | 1
>false : false
>1 : 1
>0 : 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ var result10: (t: X) => any = true ? (m) => m.propertyX1 : (n) => n.propertyX2;
//Expr1 and Expr2 are literals
var result11: any = true ? 1 : 'string';
>result11 : any
>true ? 1 : 'string' : 1 | "string"
>true ? 1 : 'string' : "string" | 1
>true : true
>1 : 1
>'string' : "string"
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/conditionalTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ function zeroOf<T extends number | string | boolean>(value: T) {
><ZeroOf<T>>(typeof value === "number" ? 0 : typeof value === "string" ? "" : false) : ZeroOf<T>
>ZeroOf : ZeroOf<T>
>T : T
>(typeof value === "number" ? 0 : typeof value === "string" ? "" : false) : false | 0 | ""
>typeof value === "number" ? 0 : typeof value === "string" ? "" : false : false | 0 | ""
>(typeof value === "number" ? 0 : typeof value === "string" ? "" : false) : false | "" | 0
>typeof value === "number" ? 0 : typeof value === "string" ? "" : false : false | "" | 0
>typeof value === "number" : boolean
>typeof value : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>value : T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var y = [() => new c()];
var k: (() => c) | string = (() => new c()) || "";
>k : string | (() => c)
>c : c
>(() => new c()) || "" : (() => c) | ""
>(() => new c()) || "" : "" | (() => c)
>(() => new c()) : () => c
>() => new c() : () => c
>new c() : c
Expand Down
63 changes: 62 additions & 1 deletion tests/baselines/reference/literalTypeWidening.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ function f5() {
let v4 = c4;
}

declare function widening<T>(x: T): T;
declare function nonWidening<T extends string | number | symbol>(x: T): T;

function f6(cond: boolean) {
let x1 = widening('a');
let x2 = widening(10);
let x3 = widening(cond ? 'a' : 10);
let y1 = nonWidening('a');
let y2 = nonWidening(10);
let y3 = nonWidening(cond ? 'a' : 10);
}

// Repro from #10898

type FAILURE = "FAILURE";
Expand Down Expand Up @@ -95,10 +107,33 @@ type TestEvent = "onmouseover" | "onmouseout";

function onMouseOver(): TestEvent { return "onmouseover"; }

let x = onMouseOver();
let x = onMouseOver();

// Repro from #23649

export function Set<K extends string>(...keys: K[]): Record<K, true | undefined> {
const result = {} as Record<K, true | undefined>
keys.forEach(key => result[key] = true)
return result
}

export function keys<K extends string, V>(obj: Record<K, V>): K[] {
return Object.keys(obj) as K[]
}

type Obj = { code: LangCode }

const langCodeSet = Set('fr', 'en', 'es', 'it', 'nl')
export type LangCode = keyof typeof langCodeSet
export const langCodes = keys(langCodeSet)

const arr: Obj[] = langCodes.map(code => ({ code }))


//// [literalTypeWidening.js]
"use strict";
// Widening vs. non-widening literal types
exports.__esModule = true;
function f1() {
var c1 = "hello"; // Widening type "hello"
var v1 = c1; // Type string
Expand Down Expand Up @@ -153,6 +188,14 @@ function f5() {
var c4 = "foo";
var v4 = c4;
}
function f6(cond) {
var x1 = widening('a');
var x2 = widening(10);
var x3 = widening(cond ? 'a' : 10);
var y1 = nonWidening('a');
var y2 = nonWidening(10);
var y3 = nonWidening(cond ? 'a' : 10);
}
var FAILURE = "FAILURE";
function doWork() {
return FAILURE;
Expand All @@ -172,3 +215,21 @@ if (isSuccess(result)) {
}
function onMouseOver() { return "onmouseover"; }
var x = onMouseOver();
// Repro from #23649
function Set() {
var keys = [];
for (var _i = 0; _i < arguments.length; _i++) {
keys[_i] = arguments[_i];
}
var result = {};
keys.forEach(function (key) { return result[key] = true; });
return result;
}
exports.Set = Set;
function keys(obj) {
return Object.keys(obj);
}
exports.keys = keys;
var langCodeSet = Set('fr', 'en', 'es', 'it', 'nl');
exports.langCodes = keys(langCodeSet);
var arr = exports.langCodes.map(function (code) { return ({ code: code }); });
Loading