diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6a1d6b52abb..d4b8c3b8a8c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 = (contextualType).types; diff --git a/tests/baselines/reference/literalIntersectionYieldsLiteral.js b/tests/baselines/reference/literalIntersectionYieldsLiteral.js new file mode 100644 index 00000000000..7781ad9bd39 --- /dev/null +++ b/tests/baselines/reference/literalIntersectionYieldsLiteral.js @@ -0,0 +1,23 @@ +//// [literalIntersectionYieldsLiteral.ts] +export type BaseAttribute = { + type?: string; +} +export type StringAttribute = BaseAttribute & { + type: "string"; +} +export type NumberAttribute = BaseAttribute & { + type: "number"; +} +export type Attribute = StringAttribute | NumberAttribute; + +const foo: Attribute = { + type: "string" +} + + +//// [literalIntersectionYieldsLiteral.js] +"use strict"; +exports.__esModule = true; +var foo = { + type: "string" +}; diff --git a/tests/baselines/reference/literalIntersectionYieldsLiteral.symbols b/tests/baselines/reference/literalIntersectionYieldsLiteral.symbols new file mode 100644 index 00000000000..2b2c2072705 --- /dev/null +++ b/tests/baselines/reference/literalIntersectionYieldsLiteral.symbols @@ -0,0 +1,35 @@ +=== tests/cases/compiler/literalIntersectionYieldsLiteral.ts === +export type BaseAttribute = { +>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 & { +>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 & { +>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)) +} + diff --git a/tests/baselines/reference/literalIntersectionYieldsLiteral.types b/tests/baselines/reference/literalIntersectionYieldsLiteral.types new file mode 100644 index 00000000000..2230f14af1a --- /dev/null +++ b/tests/baselines/reference/literalIntersectionYieldsLiteral.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/literalIntersectionYieldsLiteral.ts === +export type BaseAttribute = { +>BaseAttribute : BaseAttribute +>T : T + + type?: string; +>type : string +} +export type StringAttribute = BaseAttribute & { +>StringAttribute : StringAttribute +>BaseAttribute : BaseAttribute + + type: "string"; +>type : "string" +} +export type NumberAttribute = BaseAttribute & { +>NumberAttribute : NumberAttribute +>BaseAttribute : BaseAttribute + + 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" +} + diff --git a/tests/cases/compiler/literalIntersectionYieldsLiteral.ts b/tests/cases/compiler/literalIntersectionYieldsLiteral.ts new file mode 100644 index 00000000000..3b65201d3a3 --- /dev/null +++ b/tests/cases/compiler/literalIntersectionYieldsLiteral.ts @@ -0,0 +1,14 @@ +export type BaseAttribute = { + type?: string; +} +export type StringAttribute = BaseAttribute & { + type: "string"; +} +export type NumberAttribute = BaseAttribute & { + type: "number"; +} +export type Attribute = StringAttribute | NumberAttribute; + +const foo: Attribute = { + type: "string" +}