@@ -2242,10 +2242,49 @@ namespace ts {
2242
2242
return result;
2243
2243
}
2244
2244
2245
+ function typeFormatFlagsToNodeBuilderFlags(flags: TypeFormatFlags): NodeBuilderFlags {
2246
+ let result = NodeBuilderFlags.None;
2247
+ if (flags & TypeFormatFlags.WriteArrayAsGenericType) {
2248
+ result |= NodeBuilderFlags.WriteArrayAsGenericType;
2249
+ }
2250
+ if (flags & TypeFormatFlags.UseTypeOfFunction) {
2251
+ result |= NodeBuilderFlags.UseTypeOfFunction;
2252
+ }
2253
+ if (flags & TypeFormatFlags.NoTruncation) {
2254
+ result |= NodeBuilderFlags.NoTruncation;
2255
+ }
2256
+ if (flags & TypeFormatFlags.WriteArrowStyleSignature) {
2257
+ result |= NodeBuilderFlags.WriteArrowStyleSignature;
2258
+ }
2259
+ if (flags & TypeFormatFlags.WriteOwnNameForAnyLike) {
2260
+ result |= NodeBuilderFlags.WriteOwnNameForAnyLike;
2261
+ }
2262
+ if (flags & TypeFormatFlags.WriteArrayAsGenericType) {
2263
+ result |= NodeBuilderFlags.WriteArrayAsGenericType;
2264
+ }
2265
+ if (flags & TypeFormatFlags.WriteTypeArgumentsOfSignature) {
2266
+ result |= NodeBuilderFlags.WriteTypeArgumentsOfSignature;
2267
+ }
2268
+ if (flags & TypeFormatFlags.UseFullyQualifiedType) {
2269
+ result |= NodeBuilderFlags.UseFullyQualifiedType;
2270
+ }
2271
+ if (flags & TypeFormatFlags.UseTypeAliasValue) {
2272
+ result |= NodeBuilderFlags.UseTypeAliasValue;
2273
+ }
2274
+ if (flags & TypeFormatFlags.SuppressAnyReturnType) {
2275
+ result |= NodeBuilderFlags.SuppressAnyReturnType;
2276
+ }
2277
+ if (flags & TypeFormatFlags.AddUndefined) {
2278
+ result |= NodeBuilderFlags.AddUndefined;
2279
+ }
2280
+
2281
+ return result;
2282
+ }
2283
+
2245
2284
function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
2246
2285
const str = oldTypeToString(type, enclosingDeclaration, flags); str;
2247
2286
const str2 = oldTypeToString(type, enclosingDeclaration, flags); str2;
2248
- const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, NodeBuilderFlags.ignoreErrors);
2287
+ const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, typeFormatFlagsToNodeBuilderFlags(flags) | NodeBuilderFlags.ignoreErrors, !!(flags & TypeFormatFlags.InTypeAlias) );
2249
2288
Debug.assert(typeNode !== undefined, "should always get typenode?");
2250
2289
const newLine = NewLineKind.None;
2251
2290
const options = { newLine, removeComments: true };
@@ -2264,8 +2303,9 @@ namespace ts {
2264
2303
2265
2304
function createNodeBuilder() {
2266
2305
return {
2267
- typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => {
2306
+ typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, inTypeAlias?: boolean ) => {
2268
2307
const context = createNodeBuilderContext(enclosingDeclaration, flags);
2308
+ context.InTypeAlias = inTypeAlias;
2269
2309
const resultingNode = typeToTypeNodeHelper(type, context);
2270
2310
const result = context.encounteredError ? undefined : resultingNode;
2271
2311
return result;
@@ -2403,6 +2443,7 @@ namespace ts {
2403
2443
if (type.flags & TypeFlags.TypeParameter) {
2404
2444
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, context);
2405
2445
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
2446
+ // TODO: add constraint here?
2406
2447
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
2407
2448
}
2408
2449
@@ -2585,6 +2626,10 @@ namespace ts {
2585
2626
function typeReferenceToTypeNode(type: TypeReference) {
2586
2627
const typeArguments: Type[] = type.typeArguments || emptyArray;
2587
2628
if (type.target === globalArrayType) {
2629
+ if (context.flags & NodeBuilderFlags.WriteArrayAsGenericType) {
2630
+ const typeArgumentNode = typeToTypeNodeHelper(typeArguments[0], context);
2631
+ return createTypeReferenceNode("Array", [typeArgumentNode]);
2632
+ }
2588
2633
context.InElementType = true;
2589
2634
const elementType = typeToTypeNodeHelper(typeArguments[0], context);
2590
2635
context.InElementType = false;
0 commit comments