Skip to content

Add this/satisfies tag #1157

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

Merged
merged 9 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
44 changes: 39 additions & 5 deletions internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,22 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
} else if parent.Kind == ast.KindReturnStatement {
ret := parent.AsReturnStatement()
ret.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), ret.Expression)
ret.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), ret.Expression, true /*isAssertion*/)
} else if parent.Kind == ast.KindParenthesizedExpression {
paren := parent.AsParenthesizedExpression()
paren.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), paren.Expression)
paren.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), paren.Expression, true /*isAssertion*/)
} else if parent.Kind == ast.KindExpressionStatement &&
parent.AsExpressionStatement().Expression.Kind == ast.KindBinaryExpression {
bin := parent.AsExpressionStatement().Expression.AsBinaryExpression()
if kind := ast.GetAssignmentDeclarationKind(bin); kind != ast.JSDeclarationKindNone {
bin.Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, parent.AsExpressionStatement().Expression)
}
}
case ast.KindJSDocSatisfiesTag:
if parent.Kind == ast.KindParenthesizedExpression {
paren := parent.AsParenthesizedExpression()
paren.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocSatisfiesTag().TypeExpression, nil), paren.Expression, false /*isAssertion*/)
}
case ast.KindJSDocTemplateTag:
if fun, ok := getFunctionLikeHost(parent); ok {
if fun.TypeParameters() == nil {
Expand Down Expand Up @@ -262,6 +267,31 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
}
case ast.KindJSDocThisTag:
if fun, ok := getFunctionLikeHost(parent); ok {
Copy link
Member

Choose a reason for hiding this comment

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

What's this supposed to do for function-like-expressions that don't have a this, like arrow functions?

Copy link
Member Author

Choose a reason for hiding this comment

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

params := fun.Parameters()
if len(params) == 0 || params[0].Name().Kind != ast.KindThisKeyword {
thisParam := p.factory.NewParameterDeclaration(
nil, /* decorators */
nil, /* modifiers */
p.factory.NewIdentifier("this"),
nil, /* questionToken */
nil, /* type */
nil, /* initializer */
)
thisParam.AsParameterDeclaration().Type = p.makeNewType(tag.AsJSDocThisTag().TypeExpression, thisParam)
thisParam.Loc = tag.AsJSDocThisTag().TagName.Loc
thisParam.Flags = p.contextFlags | ast.NodeFlagsReparsed

newParams := p.nodeSlicePool.NewSlice(len(params) + 1)
newParams[0] = thisParam
for i, param := range params {
newParams[i+1] = param
}

fun.FunctionLikeData().Parameters = p.newNodeList(thisParam.Loc, newParams)
}
}
case ast.KindJSDocReturnTag:
if fun, ok := getFunctionLikeHost(parent); ok {
if fun.Type() == nil {
Expand Down Expand Up @@ -313,7 +343,6 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
}
// !!! other attached tags (@this, @satisfies) support goes here
}

func (p *Parser) makeQuestionIfOptional(parameter *ast.JSDocParameterTag) *ast.Node {
Expand Down Expand Up @@ -360,8 +389,13 @@ func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) {
return nil, false
}

func (p *Parser) makeNewTypeAssertion(t *ast.TypeNode, e *ast.Node) *ast.Node {
assert := p.factory.NewTypeAssertion(t, e)
func (p *Parser) makeNewCast(t *ast.TypeNode, e *ast.Node, isAssertion bool) *ast.Node {
var assert *ast.Node
if isAssertion {
assert = p.factory.NewTypeAssertion(t, e)
} else {
assert = p.factory.NewSatisfiesExpression(e, t)
}
assert.Flags = p.contextFlags | ast.NodeFlagsReparsed
assert.Loc = core.NewTextRange(e.Pos(), e.End())
return assert
Expand Down
2 changes: 1 addition & 1 deletion internal/testutil/tsbaseline/type_symbol_baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func forEachASTNode(node *ast.Node) []*ast.Node {
for len(work) > 0 {
elem := work[len(work)-1]
work = work[:len(work)-1]
if elem.Flags&ast.NodeFlagsReparsed != 0 && elem.Kind != ast.KindTypeAssertionExpression {
if elem.Flags&ast.NodeFlagsReparsed != 0 && elem.Kind != ast.KindTypeAssertionExpression && elem.Kind != ast.KindSatisfiesExpression {
continue
}
result = append(result, elem)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/a.js(9,26): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
/a.js(15,31): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
/a.js(23,31): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
/a.js(31,26): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.


==== /a.js (4 errors) ====
==== /a.js (2 errors) ====
class Test {
constructor() {
/** @type {number[]} */
Expand Down Expand Up @@ -34,9 +32,6 @@
/** @this {Test} */
function (d) {
console.log(d === this.data.length)
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
!!! related TS2738 /a.js:22:9: An outer value of 'this' is shadowed by this container.
}, this)
}

Expand All @@ -45,9 +40,6 @@
/** @this {Test} */
function (d) {
return d === this.data.length
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
!!! related TS2738 /a.js:30:9: An outer value of 'this' is shadowed by this container.
}, this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class Test {
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(log, Decl(lib.dom.d.ts, --, --))
>d : Symbol(d, Decl(a.js, 21, 18))
>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
>this.data : Symbol(data, Decl(a.js, 1, 19))
>this : Symbol((Missing), Decl(a.js, 20, 13))
>data : Symbol(data, Decl(a.js, 1, 19))
>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Expand All @@ -89,6 +94,11 @@ class Test {

return d === this.data.length
>d : Symbol(d, Decl(a.js, 29, 18))
>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
>this.data : Symbol(data, Decl(a.js, 1, 19))
>this : Symbol((Missing), Decl(a.js, 28, 13))
>data : Symbol(data, Decl(a.js, 1, 19))
>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
->this : Symbol(this)
->data : Symbol(Test.data, Decl(a.js, 1, 19))
->length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
+>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
+>this.data : Symbol(data, Decl(a.js, 1, 19))
+>this : Symbol((Missing), Decl(a.js, 20, 13))
+>data : Symbol(data, Decl(a.js, 1, 19))
+>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Expand All @@ -111,7 +116,7 @@

/** @this {Test} */
function (d) {
@@= skipped -64, +59 lines =@@
@@= skipped -64, +64 lines =@@

return d === this.data.length
>d : Symbol(d, Decl(a.js, 29, 18))
Expand All @@ -120,6 +125,11 @@
->this : Symbol(this)
->data : Symbol(Test.data, Decl(a.js, 1, 19))
->length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
+>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
+>this.data : Symbol(data, Decl(a.js, 1, 19))
+>this : Symbol((Missing), Decl(a.js, 28, 13))
+>data : Symbol(data, Decl(a.js, 1, 19))
+>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Test {

/** @this {Test} */
function (d) {
>function (d) { console.log(d === this.data.length) } : (d: number) => void
>function (d) { console.log(d === this.data.length) } : (this: Test, d: number) => void
>d : number

console.log(d === this.data.length)
Expand All @@ -94,11 +94,11 @@ class Test {
>log : (...data: any[]) => void
>d === this.data.length : boolean
>d : number
>this.data.length : any
>this.data : any
>this : any
>data : any
>length : any
>this.data.length : number
>this.data : number[]
>this : Test
>data : number[]
>length : number

}, this)
>this : this
Expand All @@ -117,17 +117,17 @@ class Test {

/** @this {Test} */
function (d) {
>function (d) { return d === this.data.length } : (d: number) => boolean
>function (d) { return d === this.data.length } : (this: Test, d: number) => boolean
>d : number

return d === this.data.length
>d === this.data.length : boolean
>d : number
>this.data.length : any
>this.data : any
>this : any
>data : any
>length : any
>this.data.length : number
>this.data : number[]
>this : Test
>data : number[]
>length : number

}, this)
>this : this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ const t1 = /** @satisfies {T1} */ ({ a: 1 });
>t1 : { a: number; }
>({ a: 1 }) : { a: number; }
>{ a: 1 } : { a: number; }
>{ a: 1 } : { a: number; }
>a : number
>1 : 1

const t2 = /** @satisfies {T1} */ ({ a: 1, b: 1 });
>t2 : { a: number; b: number; }
>({ a: 1, b: 1 }) : { a: number; b: number; }
>{ a: 1, b: 1 } : { a: number; b: number; }
>{ a: 1, b: 1 } : { a: number; b: number; }
>a : number
>1 : 1
>b : number
Expand All @@ -40,12 +42,14 @@ const t3 = /** @satisfies {T1} */ ({});
>t3 : {}
>({}) : {}
>{} : {}
>{} : {}

/** @type {T2} */
const t4 = /** @satisfies {T2} */ ({ a: "a" });
>t4 : T2
>({ a: "a" }) : { a: string; }
>{ a: "a" } : { a: string; }
>{ a: "a" } : { a: string; }
>a : string
>"a" : "a"

Expand All @@ -54,6 +58,7 @@ const t5 = /** @satisfies {T3} */((m) => m.substring(0));
>t5 : (m: string) => string
>((m) => m.substring(0)) : (m: string) => string
>(m) => m.substring(0) : (m: string) => string
>(m) => m.substring(0) : (m: string) => string
>m : string
>m.substring(0) : string
>m.substring : (start: number, end?: number) => string
Expand All @@ -62,23 +67,26 @@ const t5 = /** @satisfies {T3} */((m) => m.substring(0));
>0 : 0

const t6 = /** @satisfies {[number, number]} */ ([1, 2]);
>t6 : number[]
>([1, 2]) : number[]
>[1, 2] : number[]
>t6 : [number, number]
>([1, 2]) : [number, number]
>[1, 2] : [number, number]
>[1, 2] : [number, number]
>1 : 1
>2 : 2

const t7 = /** @satisfies {T4} */ ({ a: 'test' });
>t7 : { a: string; }
>({ a: 'test' }) : { a: string; }
>{ a: 'test' } : { a: string; }
>{ a: 'test' } : { a: string; }
>a : string
>'test' : "test"

const t8 = /** @satisfies {T4} */ ({ a: 'test', b: 'test' });
>t8 : { a: string; b: string; }
>({ a: 'test', b: 'test' }) : { a: string; b: string; }
>{ a: 'test', b: 'test' } : { a: string; b: string; }
>{ a: 'test', b: 'test' } : { a: string; b: string; }
>a : string
>'test' : "test"
>b : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const p = /** @satisfies {Partial<Record<Keys, unknown>>} */ ({
>p : { a: number; b: string; x: number; }
>({ a: 0, b: "hello", x: 8 // Should error, 'x' isn't in 'Keys'}) : { a: number; b: string; x: number; }
>{ a: 0, b: "hello", x: 8 // Should error, 'x' isn't in 'Keys'} : { a: number; b: string; x: number; }
>{ a: 0, b: "hello", x: 8 // Should error, 'x' isn't in 'Keys'} : { a: number; b: string; x: number; }

a: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ const t2 = /** @satisfies {number} */ (1);
>t2 : 1
>(1) : 1
>1 : 1
>1 : 1

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const t2 = /** @satisfies T1 */ ({ a: 1 });
>t2 : { a: number; }
>({ a: 1 }) : { a: number; }
>{ a: 1 } : { a: number; }
>{ a: 1 } : { a: number; }
>a : number
>1 : 1

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
/** @typedef {Object.<string, (n: number) => boolean>} Predicates */

const p = /** @satisfies {Predicates} */ ({
>p : { isEven: (n: any) => boolean; isOdd: (n: any) => boolean; }
>({ isEven: n => n % 2 === 0, isOdd: n => n % 2 === 1}) : { isEven: (n: any) => boolean; isOdd: (n: any) => boolean; }
>p : any
>({ isEven: n => n % 2 === 0, isOdd: n => n % 2 === 1}) : any
>{ isEven: n => n % 2 === 0, isOdd: n => n % 2 === 1} : any
>{ isEven: n => n % 2 === 0, isOdd: n => n % 2 === 1} : { isEven: (n: any) => boolean; isOdd: (n: any) => boolean; }

isEven: n => n % 2 === 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/a.js(4,7): error TS7006: Parameter 's' implicitly has an 'any' type.
/a.js(8,49): error TS7006: Parameter 'x' implicitly has an 'any' type.
/a.js(3,7): error TS7006: Parameter 's' implicitly has an 'any' type.


==== /a.js (2 errors) ====
==== /a.js (1 errors) ====
/** @type {{ f(s: string): void } & Record<string, unknown> }} */
let obj = /** @satisfies {{ g(s: string): void } & Record<string, unknown>} */ ({
f(s) { }, // "incorrect" implicit any on 's'
g(s) { }
~
!!! error TS7006: Parameter 's' implicitly has an 'any' type.
g(s) { }
});

// This needs to not crash (outer node is not expression)
/** @satisfies {{ f(s: string): void }} */ ({ f(x) { } })
~
!!! error TS7006: Parameter 'x' implicitly has an 'any' type.

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
/** @type {{ f(s: string): void } & Record<string, unknown> }} */
let obj = /** @satisfies {{ g(s: string): void } & Record<string, unknown>} */ ({
>obj : { f(s: string): void; } & Record<string, unknown>
>({ f(s) { }, // "incorrect" implicit any on 's' g(s) { }}) : { f(s: string): void; g(s: any): void; }
>{ f(s) { }, // "incorrect" implicit any on 's' g(s) { }} : { f(s: string): void; g(s: any): void; }
>({ f(s) { }, // "incorrect" implicit any on 's' g(s) { }}) : { f(s: any): void; g(s: string): void; }
>{ f(s) { }, // "incorrect" implicit any on 's' g(s) { }} : { f(s: any): void; g(s: string): void; }
>{ f(s) { }, // "incorrect" implicit any on 's' g(s) { }} : { f(s: any): void; g(s: string): void; }

f(s) { }, // "incorrect" implicit any on 's'
>f : (s: string) => void
>s : string
>f : (s: any) => void
>s : any

g(s) { }
>g : (s: any) => void
>s : any
>g : (s: string) => void
>s : string

});

// This needs to not crash (outer node is not expression)
/** @satisfies {{ f(s: string): void }} */ ({ f(x) { } })
>({ f(x) { } }) : { f(x: any): void; }
>{ f(x) { } } : { f(x: any): void; }
>f : (x: any) => void
>x : any
>({ f(x) { } }) : { f(x: string): void; }
>{ f(x) { } } : { f(x: string): void; }
>{ f(x) { } } : { f(x: string): void; }
>f : (x: string) => void
>x : string

Loading