Formatting a union should not create a 1-element union type node (#27582)
This commit is contained in:
parent
9ad95ef213
commit
6175e60fec
|
@ -3307,6 +3307,9 @@ namespace ts {
|
|||
}
|
||||
if (type.flags & (TypeFlags.Union | TypeFlags.Intersection)) {
|
||||
const types = type.flags & TypeFlags.Union ? formatUnionTypes((<UnionType>type).types) : (<IntersectionType>type).types;
|
||||
if (length(types) === 1) {
|
||||
return typeToTypeNodeHelper(types[0], context);
|
||||
}
|
||||
const typeNodes = mapToTypeNodes(types, context, /*isBareList*/ true);
|
||||
if (typeNodes && typeNodes.length > 0) {
|
||||
const unionOrIntersectionTypeNode = createUnionOrIntersectionTypeNode(type.flags & TypeFlags.Union ? SyntaxKind.UnionType : SyntaxKind.IntersectionType, typeNodes);
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
//// [declarationEmitNoNonRequiredParens.ts]
|
||||
export enum Test {
|
||||
A, B, C
|
||||
}
|
||||
|
||||
export type TestType = typeof Test;
|
||||
|
||||
export const bar = (null as TestType[Extract<keyof TestType, string>][]);
|
||||
|
||||
//// [declarationEmitNoNonRequiredParens.js]
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
var Test;
|
||||
(function (Test) {
|
||||
Test[Test["A"] = 0] = "A";
|
||||
Test[Test["B"] = 1] = "B";
|
||||
Test[Test["C"] = 2] = "C";
|
||||
})(Test = exports.Test || (exports.Test = {}));
|
||||
exports.bar = null;
|
||||
|
||||
|
||||
//// [declarationEmitNoNonRequiredParens.d.ts]
|
||||
export declare enum Test {
|
||||
A = 0,
|
||||
B = 1,
|
||||
C = 2
|
||||
}
|
||||
export declare type TestType = typeof Test;
|
||||
export declare const bar: Test[];
|
|
@ -0,0 +1,20 @@
|
|||
=== tests/cases/compiler/declarationEmitNoNonRequiredParens.ts ===
|
||||
export enum Test {
|
||||
>Test : Symbol(Test, Decl(declarationEmitNoNonRequiredParens.ts, 0, 0))
|
||||
|
||||
A, B, C
|
||||
>A : Symbol(Test.A, Decl(declarationEmitNoNonRequiredParens.ts, 0, 18))
|
||||
>B : Symbol(Test.B, Decl(declarationEmitNoNonRequiredParens.ts, 1, 6))
|
||||
>C : Symbol(Test.C, Decl(declarationEmitNoNonRequiredParens.ts, 1, 9))
|
||||
}
|
||||
|
||||
export type TestType = typeof Test;
|
||||
>TestType : Symbol(TestType, Decl(declarationEmitNoNonRequiredParens.ts, 2, 1))
|
||||
>Test : Symbol(Test, Decl(declarationEmitNoNonRequiredParens.ts, 0, 0))
|
||||
|
||||
export const bar = (null as TestType[Extract<keyof TestType, string>][]);
|
||||
>bar : Symbol(bar, Decl(declarationEmitNoNonRequiredParens.ts, 6, 12))
|
||||
>TestType : Symbol(TestType, Decl(declarationEmitNoNonRequiredParens.ts, 2, 1))
|
||||
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
|
||||
>TestType : Symbol(TestType, Decl(declarationEmitNoNonRequiredParens.ts, 2, 1))
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
=== tests/cases/compiler/declarationEmitNoNonRequiredParens.ts ===
|
||||
export enum Test {
|
||||
>Test : Test
|
||||
|
||||
A, B, C
|
||||
>A : Test.A
|
||||
>B : Test.B
|
||||
>C : Test.C
|
||||
}
|
||||
|
||||
export type TestType = typeof Test;
|
||||
>TestType : typeof Test
|
||||
>Test : typeof Test
|
||||
|
||||
export const bar = (null as TestType[Extract<keyof TestType, string>][]);
|
||||
>bar : Test[]
|
||||
>(null as TestType[Extract<keyof TestType, string>][]) : Test[]
|
||||
>null as TestType[Extract<keyof TestType, string>][] : Test[]
|
||||
>null : null
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// @declaration: true
|
||||
export enum Test {
|
||||
A, B, C
|
||||
}
|
||||
|
||||
export type TestType = typeof Test;
|
||||
|
||||
export const bar = (null as TestType[Extract<keyof TestType, string>][]);
|
Loading…
Reference in a new issue