-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generate names for type parameter declarations in inferred types #23902
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
Changes from 2 commits
68aac01
37cedfe
3844436
8278039
ae14266
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,6 +197,15 @@ namespace ts { | |
return name; | ||
} | ||
|
||
export function getOptimisticScopedGeneratedNameForName(node: Identifier): Identifier { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a special factory function for this? Couldn't we just add the extra flags to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably? But I'd also probably still write that into a function so it stays as an inline call as the use site. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather we just change the signature of
Also, the existing |
||
const name = createIdentifier(idText(node)); | ||
name.autoGenerateFlags = GeneratedIdentifierFlags.Node | GeneratedIdentifierFlags.Optimistic | GeneratedIdentifierFlags.ReservedInNestedScopes; | ||
name.autoGenerateId = nextAutoGenerateId; | ||
name.original = node; | ||
nextAutoGenerateId++; | ||
return name; | ||
} | ||
|
||
// Punctuation | ||
|
||
export function createToken<TKind extends SyntaxKind>(token: TKind) { | ||
|
@@ -4228,6 +4237,7 @@ namespace ts { | |
switch (member.kind) { | ||
case SyntaxKind.TypeQuery: | ||
case SyntaxKind.TypeOperator: | ||
case SyntaxKind.InferType: | ||
return createParenthesizedType(member); | ||
} | ||
return parenthesizeElementTypeMember(member); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3031,7 +3031,7 @@ namespace ts { | |
// Options | ||
NoTruncation = 1 << 0, // Don't truncate result | ||
WriteArrayAsGenericType = 1 << 1, // Write Array<T> instead T[] | ||
// empty space | ||
GenerateNamesForShadowedTypeParams = 1 << 2, // When a type parameter T is shadowing another T, generate a name for it so it can still be referenced | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this need a flag? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't wanna bother renaming the conflicts in quickinfo and signature help, at least in my opinion. |
||
UseStructuralFallback = 1 << 3, // When an alias cannot be named by its symbol, rather than report an error, fallback to a structural printout if possible | ||
// empty space | ||
WriteTypeArgumentsOfSignature = 1 << 5, // Write the type arguments instead of type parameters of the signature | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1853,6 +1853,7 @@ declare namespace ts { | |
None = 0, | ||
NoTruncation = 1, | ||
WriteArrayAsGenericType = 2, | ||
GenerateNamesForShadowedTypeParams = 4, | ||
UseStructuralFallback = 8, | ||
WriteTypeArgumentsOfSignature = 32, | ||
UseFullyQualifiedType = 64, | ||
|
@@ -3444,6 +3445,7 @@ declare namespace ts { | |
function createFileLevelUniqueName(text: string): Identifier; | ||
/** Create a unique name generated for a node. */ | ||
function getGeneratedNameForNode(node: Node): Identifier; | ||
function getOptimisticScopedGeneratedNameForName(node: Identifier): Identifier; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be public? |
||
function createToken<TKind extends SyntaxKind>(token: TKind): Token<TKind>; | ||
function createSuper(): SuperExpression; | ||
function createThis(): ThisExpression & Token<SyntaxKind.ThisKeyword>; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//// [declarationEmitNestedGenerics.ts] | ||
function f<T>(p: T) { | ||
let g: <T>(x: T) => typeof p = null as any; | ||
return g; | ||
} | ||
|
||
function g<T>(x: T) { | ||
let y: typeof x extends (infer T)[] ? T : typeof x = null as any; | ||
return y; | ||
} | ||
|
||
//// [declarationEmitNestedGenerics.js] | ||
function f(p) { | ||
var g = null; | ||
return g; | ||
} | ||
function g(x) { | ||
var y = null; | ||
return y; | ||
} | ||
|
||
|
||
//// [declarationEmitNestedGenerics.d.ts] | ||
declare function f<T>(p: T): <T_1>(x: T_1) => T; | ||
declare function g<T>(x: T): T extends (infer T_1)[] ? T_1 : T; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anything that goes in
generatedNames
is already reserved throughout the entire generated output, so scoping these doesn't seem to make any sense. Why was this needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scoped
causes it to go intoreservedNames
instead ofgeneratedNames
; this allows, eg,T_1
to be reused in adjacent scopes.