@@ -33,6 +33,7 @@ import {
33
33
Type ,
34
34
ValidatedIdentifier ,
35
35
ValueKind ,
36
+ getHookKindForType ,
36
37
makeBlockId ,
37
38
makeIdentifierId ,
38
39
makeIdentifierName ,
@@ -787,13 +788,23 @@ export class Environment {
787
788
( isHookName ( binding . imported ) ? this . #getCustomHookType( ) : null )
788
789
) ;
789
790
} else {
791
+ const expectHook =
792
+ isHookName ( binding . imported ) || isHookName ( binding . name ) ;
790
793
const moduleType = this . #resolveModuleType( binding . module , loc ) ;
791
794
if ( moduleType !== null ) {
792
795
const importedType = this . getPropertyType (
793
796
moduleType ,
794
797
binding . imported ,
795
798
) ;
796
799
if ( importedType != null ) {
800
+ const isHook = getHookKindForType ( this , importedType ) != null ;
801
+ if ( expectHook !== isHook ) {
802
+ CompilerError . throwInvalidConfig ( {
803
+ reason : `Invalid type configuration for module` ,
804
+ description : `Expected type for '${ binding . module } .${ binding . imported } ' to ${ expectHook ? 'be a hook' : 'not be a hook' } based on its name` ,
805
+ loc,
806
+ } ) ;
807
+ }
797
808
return importedType ;
798
809
}
799
810
}
@@ -806,9 +817,7 @@ export class Environment {
806
817
* `import {useHook as foo} ...`
807
818
* `import {foo as useHook} ...`
808
819
*/
809
- return isHookName ( binding . imported ) || isHookName ( binding . name )
810
- ? this . #getCustomHookType( )
811
- : null ;
820
+ return expectHook ? this . #getCustomHookType( ) : null ;
812
821
}
813
822
}
814
823
case 'ImportDefault' :
@@ -820,18 +829,31 @@ export class Environment {
820
829
( isHookName ( binding . name ) ? this . #getCustomHookType( ) : null )
821
830
) ;
822
831
} else {
832
+ const expectHook = isHookName ( binding . name ) ;
823
833
const moduleType = this . #resolveModuleType( binding . module , loc ) ;
824
834
if ( moduleType !== null ) {
835
+ let importedType : Type | null = null ;
825
836
if ( binding . kind === 'ImportDefault' ) {
826
837
const defaultType = this . getPropertyType ( moduleType , 'default' ) ;
827
838
if ( defaultType !== null ) {
828
- return defaultType ;
839
+ importedType = defaultType ;
829
840
}
830
841
} else {
831
- return moduleType ;
842
+ importedType = moduleType ;
843
+ }
844
+ if ( importedType !== null ) {
845
+ const isHook = getHookKindForType ( this , importedType ) != null ;
846
+ if ( expectHook !== isHook ) {
847
+ CompilerError . throwInvalidConfig ( {
848
+ reason : `Invalid type configuration for module` ,
849
+ description : `Expected type for '${ binding . module } ' (as '${ binding . name } ') to ${ expectHook ? 'be a hook' : 'not be a hook' } based on its name` ,
850
+ loc,
851
+ } ) ;
852
+ }
853
+ return importedType ;
832
854
}
833
855
}
834
- return isHookName ( binding . name ) ? this . #getCustomHookType( ) : null ;
856
+ return expectHook ? this . #getCustomHookType( ) : null ;
835
857
}
836
858
}
837
859
}
0 commit comments