@@ -116,6 +116,8 @@ export class ScriptLetContext {
116116
117117 private readonly usedUniqueIds = new Set < string > ( ) ;
118118
119+ private storeValueTypeName : string | undefined ;
120+
119121 public constructor ( ctx : Context ) {
120122 this . script = ctx . sourceCode . scripts ;
121123 this . ctx = ctx ;
@@ -537,8 +539,27 @@ export class ScriptLetContext {
537539 for ( const nm of maybeStores ) {
538540 if ( reservedNames . has ( nm ) ) continue ;
539541
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+
540561 this . appendScriptWithoutOffset (
541- `declare let $${ nm } : Parameters<Parameters<( typeof ${ nm } )["subscribe"]>[0]>[0] ;` ,
562+ `declare let $${ nm } : ${ this . storeValueTypeName } < typeof ${ nm } > ;` ,
542563 ( node , tokens , comments , result ) => {
543564 tokens . length = 0 ;
544565 comments . length = 0 ;
@@ -933,9 +954,9 @@ function removeAllScope(target: ESTree.Node, result: ScriptLetCallbackOption) {
933954 }
934955 if ( node . type === "Identifier" ) {
935956 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 ]
939960 ) {
940961 scope = scope . upper ! ;
941962 }
0 commit comments