@@ -116,6 +116,8 @@ export class ScriptLetContext {
116
116
117
117
private readonly usedUniqueIds = new Set < string > ( ) ;
118
118
119
+ private storeValueTypeName : string | undefined ;
120
+
119
121
public constructor ( ctx : Context ) {
120
122
this . script = ctx . sourceCode . scripts ;
121
123
this . ctx = ctx ;
@@ -537,8 +539,27 @@ export class ScriptLetContext {
537
539
for ( const nm of maybeStores ) {
538
540
if ( reservedNames . has ( nm ) ) continue ;
539
541
542
+ if ( ! this . storeValueTypeName ) {
543
+ this . storeValueTypeName = this . generateUniqueId ( "StoreValueType" ) ;
544
+
545
+ this . appendScriptWithoutOffset (
546
+ `type ${ this . storeValueTypeName } <T> = T extends null | undefined
547
+ ? T
548
+ : T extends object & { subscribe(run: infer F, ...args: any): any }
549
+ ? F extends (value: infer V, ...args: any) => any
550
+ ? V
551
+ : never
552
+ : T;` ,
553
+ ( node , tokens , comments , result ) => {
554
+ tokens . length = 0 ;
555
+ comments . length = 0 ;
556
+ removeAllScope ( node , result ) ;
557
+ }
558
+ ) ;
559
+ }
560
+
540
561
this . appendScriptWithoutOffset (
541
- `declare let $${ nm } : Parameters<Parameters<( typeof ${ nm } )["subscribe"]>[0]>[0] ;` ,
562
+ `declare let $${ nm } : ${ this . storeValueTypeName } < typeof ${ nm } > ;` ,
542
563
( node , tokens , comments , result ) => {
543
564
tokens . length = 0 ;
544
565
comments . length = 0 ;
@@ -933,9 +954,9 @@ function removeAllScope(target: ESTree.Node, result: ScriptLetCallbackOption) {
933
954
}
934
955
if ( node . type === "Identifier" ) {
935
956
let scope = result . getInnermostScope ( node ) ;
936
- if (
937
- ( scope . block as any ) . type === "TSTypeAliasDeclaration" &&
938
- ( scope . block as any ) . id === node
957
+ while (
958
+ target . range ! [ 0 ] <= scope . block . range ! [ 0 ] &&
959
+ scope . block . range ! [ 1 ] <= target . range ! [ 1 ]
939
960
) {
940
961
scope = scope . upper ! ;
941
962
}
0 commit comments