1- import { type TBabel } from './index.d' ;
2- class Ctx {
1+ import type { TransformOptions } from '@babel/core' ;
2+ import type { TBabel , TReact , IStringToReactApi } from './index.d' ;
3+ import React , { FC , PropsWithChildren } from 'react' ;
4+ class Ctx implements IStringToReactApi {
35 _temp : string = '' ;
46 _parentTemp : string = `"use strict";\nreturn @temp;` ;
5- _com : Function | null = null ;
7+ _com : FC < PropsWithChildren < { } > > = function ( ) {
8+ return null ;
9+ } ;
610 _getBabel : ( ) => TBabel ;
7- constructor ( React : object , Babel : TBabel ) {
11+ constructor ( React : TReact , Babel : TBabel ) {
812 window . React = window . React || React ;
913 if ( ! Babel ) {
1014 throw new Error (
@@ -13,23 +17,23 @@ class Ctx {
1317 }
1418 this . _getBabel = ( ) => Babel ;
1519 }
16- _checkBabelOptions ( babelOptions : any ) {
20+ _checkBabelOptions ( babelOptions : TransformOptions ) {
1721 if ( Object . prototype . toString . call ( babelOptions ) !== '[object Object]' ) {
1822 throw new Error ( `babelOptions prop of string-to-react-component element should be an object.` ) ;
1923 }
2024 if ( Object . prototype . hasOwnProperty . call ( babelOptions , 'presets' ) === false ) {
2125 babelOptions . presets = [ 'react' ] ;
2226 } else {
2327 //check if babelOptions.presets is not type of Array
24- if ( ! ( typeof babelOptions . presets === 'object' && babelOptions . presets . constructor == Array ) ) {
28+ if ( ! ( typeof babelOptions . presets === 'object' && babelOptions . presets ? .constructor == Array ) ) {
2529 throw new Error ( `string-to-react-component Error : presets property of babelOptions prop should be an array` ) ;
2630 }
2731 if ( babelOptions . presets . indexOf ( 'react' ) === - 1 ) {
2832 babelOptions . presets . push ( 'react' ) ;
2933 }
3034 }
3135 }
32- _transpile ( babelOptions : any ) : string {
36+ _transpile ( babelOptions : TransformOptions ) : string {
3337 // make sure react presets is registered in babelOptions
3438 this . _checkBabelOptions ( babelOptions ) ;
3539 const resultObj = this . _getBabel ( ) . transform ( this . _temp , babelOptions ) ;
@@ -57,15 +61,15 @@ class Ctx {
5761 throw new Error ( `passed string into string-to-react-component element can not be empty` ) ;
5862 }
5963 }
60- updateTemplate ( template : any , babelOptions : any ) {
64+ updateTemplate ( template : string , babelOptions : TransformOptions ) : IStringToReactApi {
6165 this . _validateTemplate ( template ) ;
6266 if ( template !== this . _temp ) {
6367 this . _temp = template ;
6468 this . _generateCom ( babelOptions ) ;
6569 }
6670 return this ;
6771 }
68- getComponent ( ) {
72+ getComponent ( ) : FC < PropsWithChildren < { } > > {
6973 return this . _com ;
7074 }
7175}
0 commit comments