Use TypeFlags.UnionOrIntersection not TypeFlags.Union (#20662)

This commit is contained in:
Wesley Wigham
2017-12-12 16:32:55 -08:00
committed by GitHub
parent 5dab24a2a7
commit 40029a0523
5 changed files with 110 additions and 1 deletions

View File

@@ -19094,7 +19094,7 @@ namespace ts {
function isLiteralOfContextualType(candidateType: Type, contextualType: Type): boolean {
if (contextualType) {
if (contextualType.flags & TypeFlags.Union && !(contextualType.flags & TypeFlags.Boolean)) {
if (contextualType.flags & TypeFlags.UnionOrIntersection && !(contextualType.flags & TypeFlags.Boolean)) {
// If the contextual type is a union containing both of the 'true' and 'false' types we
// don't consider it a literal context for boolean literals.
const types = (<UnionType>contextualType).types;

View File

@@ -0,0 +1,23 @@
//// [literalIntersectionYieldsLiteral.ts]
export type BaseAttribute<T> = {
type?: string;
}
export type StringAttribute = BaseAttribute<string> & {
type: "string";
}
export type NumberAttribute = BaseAttribute<string> & {
type: "number";
}
export type Attribute = StringAttribute | NumberAttribute;
const foo: Attribute = {
type: "string"
}
//// [literalIntersectionYieldsLiteral.js]
"use strict";
exports.__esModule = true;
var foo = {
type: "string"
};

View File

@@ -0,0 +1,35 @@
=== tests/cases/compiler/literalIntersectionYieldsLiteral.ts ===
export type BaseAttribute<T> = {
>BaseAttribute : Symbol(BaseAttribute, Decl(literalIntersectionYieldsLiteral.ts, 0, 0))
>T : Symbol(T, Decl(literalIntersectionYieldsLiteral.ts, 0, 26))
type?: string;
>type : Symbol(type, Decl(literalIntersectionYieldsLiteral.ts, 0, 32))
}
export type StringAttribute = BaseAttribute<string> & {
>StringAttribute : Symbol(StringAttribute, Decl(literalIntersectionYieldsLiteral.ts, 2, 1))
>BaseAttribute : Symbol(BaseAttribute, Decl(literalIntersectionYieldsLiteral.ts, 0, 0))
type: "string";
>type : Symbol(type, Decl(literalIntersectionYieldsLiteral.ts, 3, 55))
}
export type NumberAttribute = BaseAttribute<string> & {
>NumberAttribute : Symbol(NumberAttribute, Decl(literalIntersectionYieldsLiteral.ts, 5, 1))
>BaseAttribute : Symbol(BaseAttribute, Decl(literalIntersectionYieldsLiteral.ts, 0, 0))
type: "number";
>type : Symbol(type, Decl(literalIntersectionYieldsLiteral.ts, 6, 55))
}
export type Attribute = StringAttribute | NumberAttribute;
>Attribute : Symbol(Attribute, Decl(literalIntersectionYieldsLiteral.ts, 8, 1))
>StringAttribute : Symbol(StringAttribute, Decl(literalIntersectionYieldsLiteral.ts, 2, 1))
>NumberAttribute : Symbol(NumberAttribute, Decl(literalIntersectionYieldsLiteral.ts, 5, 1))
const foo: Attribute = {
>foo : Symbol(foo, Decl(literalIntersectionYieldsLiteral.ts, 11, 5))
>Attribute : Symbol(Attribute, Decl(literalIntersectionYieldsLiteral.ts, 8, 1))
type: "string"
>type : Symbol(type, Decl(literalIntersectionYieldsLiteral.ts, 11, 24))
}

View File

@@ -0,0 +1,37 @@
=== tests/cases/compiler/literalIntersectionYieldsLiteral.ts ===
export type BaseAttribute<T> = {
>BaseAttribute : BaseAttribute<T>
>T : T
type?: string;
>type : string
}
export type StringAttribute = BaseAttribute<string> & {
>StringAttribute : StringAttribute
>BaseAttribute : BaseAttribute<T>
type: "string";
>type : "string"
}
export type NumberAttribute = BaseAttribute<string> & {
>NumberAttribute : NumberAttribute
>BaseAttribute : BaseAttribute<T>
type: "number";
>type : "number"
}
export type Attribute = StringAttribute | NumberAttribute;
>Attribute : Attribute
>StringAttribute : StringAttribute
>NumberAttribute : NumberAttribute
const foo: Attribute = {
>foo : Attribute
>Attribute : Attribute
>{ type: "string"} : { type: "string"; }
type: "string"
>type : string
>"string" : "string"
}

View File

@@ -0,0 +1,14 @@
export type BaseAttribute<T> = {
type?: string;
}
export type StringAttribute = BaseAttribute<string> & {
type: "string";
}
export type NumberAttribute = BaseAttribute<string> & {
type: "number";
}
export type Attribute = StringAttribute | NumberAttribute;
const foo: Attribute = {
type: "string"
}