Skip to content
Merged
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
12 changes: 10 additions & 2 deletions internal/checker/pseudotypenodebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,21 @@ func (b *NodeBuilderImpl) pseudoTypeToNode(t *pseudochecker.PseudoType) *ast.Nod
switch e.Kind {
case pseudochecker.PseudoObjectElementKindMethod:
d := e.AsPseudoObjectMethod()
var typeParams *ast.NodeList
if len(d.TypeParameters) > 0 {
res := make([]*ast.Node, 0, len(d.TypeParameters))
for _, tp := range d.TypeParameters {
res = append(res, b.reuseNode(tp.AsNode()))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was suspicious of this, but it's the same thing we do for pseudochecker.PseudoTypeKindSingleCallSignature just above here.

I do wonder what would happen if you had a type parameter that say extends typeof local. Do we handle that in general?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I do wonder what would happen if you had a type parameter that say extends typeof local. Do we handle that in general?

Yeah, that's handled in general - unless you have some specific weird edge in mind that could break things with ID or without it.

typeof x is nice because it has to refer to an existing entity name and the declaration emitter also preserves the referenced name (and emits its type ofc) when needed even if that name is not exported otherwise. Local-local names in closures are usually just replaced with their types. In ID it's also kinda hard to even somehow try to "leak" a local-local to the top-level scope because of:

Function must have an explicit return type annotation with --isolatedDeclarations.

}
typeParams = b.f.NewNodeList(res)
}
if isConst {
newProp = b.f.NewPropertySignatureDeclaration(
modifiers,
b.reuseName(e.Name),
nil,
b.f.NewFunctionTypeNode(
nil,
typeParams,
b.pseudoParametersToNodeList(d.Parameters),
b.pseudoTypeToNode(d.ReturnType),
),
Expand All @@ -186,7 +194,7 @@ func (b *NodeBuilderImpl) pseudoTypeToNode(t *pseudochecker.PseudoType) *ast.Nod
modifiers,
b.reuseName(e.Name),
nil,
nil,
typeParams,
b.pseudoParametersToNodeList(d.Parameters),
b.pseudoTypeToNode(d.ReturnType),
)
Expand Down
1 change: 1 addition & 0 deletions internal/pseudochecker/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func (ch *PseudoChecker) typeFromObjectLiteral(node *ast.ObjectLiteralExpression
e,
e.Name(),
optional,
ch.cloneTypeParameters(e.AsMethodDeclaration().TypeParameters),
ch.cloneParameters(e.ParameterList()),
ch.createReturnFromSignature(e),
))
Expand Down
16 changes: 9 additions & 7 deletions internal/pseudochecker/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,18 @@ func newPseudoObjectElement(kind PseudoObjectElementKind, name *ast.Node, option

type PseudoObjectMethod struct {
PseudoObjectElement
Signature *ast.Node
Parameters []*PseudoParameter
ReturnType *PseudoType
Signature *ast.Node
TypeParameters []*ast.TypeParameterDeclaration
Parameters []*PseudoParameter
ReturnType *PseudoType
}

func NewPseudoObjectMethod(signature *ast.Node, name *ast.Node, optional bool, parameters []*PseudoParameter, returnType *PseudoType) *PseudoObjectElement {
func NewPseudoObjectMethod(signature *ast.Node, name *ast.Node, optional bool, typeParameters []*ast.TypeParameterDeclaration, parameters []*PseudoParameter, returnType *PseudoType) *PseudoObjectElement {
return newPseudoObjectElement(PseudoObjectElementKindMethod, name, optional, &PseudoObjectMethod{
Signature: signature,
Parameters: parameters,
ReturnType: returnType,
Signature: signature,
TypeParameters: typeParameters,
Parameters: parameters,
ReturnType: returnType,
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/declarationEmitConstObjectLiteralGenericMethod1.ts] ////

//// [declarationEmitConstObjectLiteralGenericMethod1.ts]
export const obj1 = {
id<T>(value: T) {
return value;
},
pair<T>(left: T, right: T) {
return [left, right];
},
} as const;


//// [declarationEmitConstObjectLiteralGenericMethod1.js]
export const obj1 = {
id(value) {
return value;
},
pair(left, right) {
return [left, right];
},
};


//// [declarationEmitConstObjectLiteralGenericMethod1.d.ts]
export declare const obj1: {
id<T>(value: T): T;
pair<T_1>(left: T_1, right: T_1): T_1[];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/declarationEmitConstObjectLiteralGenericMethod1.ts] ////

=== declarationEmitConstObjectLiteralGenericMethod1.ts ===
export const obj1 = {
>obj1 : Symbol(obj1, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 0, 12))

id<T>(value: T) {
>id : Symbol(id, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 0, 21))
>T : Symbol(T, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 1, 7))
>value : Symbol(value, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 1, 10))
>T : Symbol(T, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 1, 7))

return value;
>value : Symbol(value, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 1, 10))

},
pair<T>(left: T, right: T) {
>pair : Symbol(pair, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 3, 6))
>T : Symbol(T, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 4, 9))
>left : Symbol(left, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 4, 12))
>T : Symbol(T, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 4, 9))
>right : Symbol(right, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 4, 20))
>T : Symbol(T, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 4, 9))

return [left, right];
>left : Symbol(left, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 4, 12))
>right : Symbol(right, Decl(declarationEmitConstObjectLiteralGenericMethod1.ts, 4, 20))

},
} as const;
>const : Symbol(const)

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/declarationEmitConstObjectLiteralGenericMethod1.ts] ////

=== declarationEmitConstObjectLiteralGenericMethod1.ts ===
export const obj1 = {
>obj1 : { readonly id: <T>(value: T) => T; readonly pair: <T>(left: T, right: T) => T[]; }
>{ id<T>(value: T) { return value; }, pair<T>(left: T, right: T) { return [left, right]; },} as const : { readonly id: <T>(value: T) => T; readonly pair: <T>(left: T, right: T) => T[]; }
>{ id<T>(value: T) { return value; }, pair<T>(left: T, right: T) { return [left, right]; },} : { readonly id: <T>(value: T) => T; readonly pair: <T>(left: T, right: T) => T[]; }

id<T>(value: T) {
>id : <T>(value: T) => T
>value : T

return value;
>value : T

},
pair<T>(left: T, right: T) {
>pair : <T>(left: T, right: T) => T[]
>left : T
>right : T

return [left, right];
>[left, right] : T[]
>left : T
>right : T

},
} as const;

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @declaration: true

export const obj1 = {
id<T>(value: T) {
return value;
},
pair<T>(left: T, right: T) {
return [left, right];
},
} as const;
Loading