Skip to content

[Release-2.0.5] Fix 11035: missing generic in .d.ts emit #11154

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

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 8 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2121,12 +2121,16 @@ namespace ts {
}

function buildTypeDisplay(type: Type, writer: SymbolWriter, enclosingDeclaration?: Node, globalFlags?: TypeFormatFlags, symbolStack?: Symbol[]) {
const globalFlagsToPass = globalFlags & TypeFormatFlags.WriteOwnNameForAnyLike;
let globalFlagsToPass = globalFlags & TypeFormatFlags.WriteOwnNameForAnyLike;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let globalFlagsToPass = globalFlags & (TypeFormatFlags.WriteOwnNameForAnyLike | TypeFormatFlags.UseTypeAliasValue);```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then that will make we serialize type-alias all the time. THough we won't want to do that for quickinfo I think. because in if we want to serialize type-alias all the time, we can just remove the check completely

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let globalFlagsToPass = globalFlags & (TypeFormatFlags.WriteOwnNameForAnyLike | TypeFormatFlags.UseTypeAliasValue);```

Is similar change needed in master?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not at all. So this is just address an issue for release-2.0.5 the master uses accessibility check (check accessibility allows we to correctly check local type)

if (globalFlags & TypeFormatFlags.UseTypeAliasValue) {
globalFlagsToPass |= TypeFormatFlags.UseTypeAliasValue;
}
let inObjectTypeLiteral = false;
return writeType(type, globalFlags);

function writeType(type: Type, flags: TypeFormatFlags) {
const nextFlags = flags & ~TypeFormatFlags.InTypeAlias;

// Write undefined/null type as any
if (type.flags & TypeFlags.Intrinsic) {
// Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving
Expand All @@ -2152,14 +2156,9 @@ namespace ts {
// The specified symbol flags need to be reinterpreted as type flags
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
}
else if (!(flags & TypeFormatFlags.InTypeAlias) && type.flags & (TypeFlags.Anonymous | TypeFlags.UnionOrIntersection) && type.aliasSymbol) {
if (type.flags & TypeFlags.Anonymous || !(flags & TypeFormatFlags.UseTypeAliasValue)) {
const typeArguments = type.aliasTypeArguments;
writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags);
}
else {
writeUnionOrIntersectionType(<UnionOrIntersectionType>type, nextFlags);
}
else if (!(flags & TypeFormatFlags.InTypeAlias) && type.flags & (TypeFlags.Anonymous | TypeFlags.UnionOrIntersection) && type.aliasSymbol && !(flags & TypeFormatFlags.UseTypeAliasValue)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again can we combine InTypeAlias and UseTypeAliasValue with |

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand what you mean. 😅 I will catch you offline tmr. I think it may be better to just serialize type-alias all the time

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again can we combine InTypeAlias and UseTypeAliasValue with |

const typeArguments = type.aliasTypeArguments;
writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags);
}
else if (type.flags & TypeFlags.UnionOrIntersection) {
writeUnionOrIntersectionType(<UnionOrIntersectionType>type, nextFlags);
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/declFileTypeAliasWithGeneric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [declFileTypeAliasWithGeneric.ts]
export type Bar<X, Y> = () => [X, Y];
export type Foo<Y> = Bar<any, Y>;
export const y = (x: Foo<string>) => 1

//// [declFileTypeAliasWithGeneric.js]
"use strict";
exports.y = function (x) { return 1; };


//// [declFileTypeAliasWithGeneric.d.ts]
export declare type Bar<X, Y> = () => [X, Y];
export declare type Foo<Y> = Bar<any, Y>;
export declare const y: (x: () => [any, string]) => number;
19 changes: 19 additions & 0 deletions tests/baselines/reference/declFileTypeAliasWithGeneric.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/declFileTypeAliasWithGeneric.ts ===
export type Bar<X, Y> = () => [X, Y];
>Bar : Symbol(Bar, Decl(declFileTypeAliasWithGeneric.ts, 0, 0))
>X : Symbol(X, Decl(declFileTypeAliasWithGeneric.ts, 0, 16))
>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 0, 18))
>X : Symbol(X, Decl(declFileTypeAliasWithGeneric.ts, 0, 16))
>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 0, 18))

export type Foo<Y> = Bar<any, Y>;
>Foo : Symbol(Foo, Decl(declFileTypeAliasWithGeneric.ts, 0, 37))
>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 1, 16))
>Bar : Symbol(Bar, Decl(declFileTypeAliasWithGeneric.ts, 0, 0))
>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 1, 16))

export const y = (x: Foo<string>) => 1
>y : Symbol(y, Decl(declFileTypeAliasWithGeneric.ts, 2, 12))
>x : Symbol(x, Decl(declFileTypeAliasWithGeneric.ts, 2, 18))
>Foo : Symbol(Foo, Decl(declFileTypeAliasWithGeneric.ts, 0, 37))

21 changes: 21 additions & 0 deletions tests/baselines/reference/declFileTypeAliasWithGeneric.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/declFileTypeAliasWithGeneric.ts ===
export type Bar<X, Y> = () => [X, Y];
>Bar : Bar<X, Y>
>X : X
>Y : Y
>X : X
>Y : Y

export type Foo<Y> = Bar<any, Y>;
>Foo : Bar<any, Y>
>Y : Y
>Bar : Bar<X, Y>
>Y : Y

export const y = (x: Foo<string>) => 1
>y : (x: Bar<string>) => number
>(x: Foo<string>) => 1 : (x: Bar<string>) => number
>x : Bar<string>
>Foo : Bar<any, Y>
>1 : number

4 changes: 4 additions & 0 deletions tests/cases/compiler/declFileTypeAliasWithGeneric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @declaration: true
export type Bar<X, Y> = () => [X, Y];
export type Foo<Y> = Bar<any, Y>;
export const y = (x: Foo<string>) => 1