File tree Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Original file line number Diff line number Diff line change 1+ # TypeScript Sample: Mixing TypeScript and JavaScript  
2+ 
3+ ## Overview   
4+ 
5+ A sample of how to use the ` allowJS `  option to use both JavaScript and TypeScript together.
6+ A simple text formatter is provided, written in JavaScript. This formatter is then used
7+ within a TypeScript class to format a computation.
8+ 
9+ To run this sample, you must have ` node `  installed. You can also use ` ts-node `  to run this directly
10+ without a compilation from TypeScript to JavaScript.
11+ 
12+ ## Running   
13+ 
14+ ``` bash 
15+ $ tsc robot.ts` 
16+ $ node robot.js`  
17+ ``` 
Original file line number Diff line number Diff line change 1+ const  surroundWithStars  =  ( value )  =>  { 
2+   const  valueLength  =  value . toString ( ) . length ; 
3+   const  topBottomBorder  =  '*' . repeat ( valueLength  +  2 ) ; 
4+ 
5+   return  topBottomBorder 
6+          +  "\n" 
7+          +  '*'  +  value . toString ( )  +  '*' 
8+          +  "\n" 
9+          +  topBottomBorder ; 
10+ } 
11+ 
12+ module . exports . Formatter  =  {  surroundWithStars } ; 
13+ 
Original file line number Diff line number Diff line change 1+ // This import wouldn't be possible without the allowJS option in tsconfig 
2+ import  {  Formatter  }  from  './format.js' ;  
3+ 
4+ interface  Robot  { 
5+   name : String ; 
6+   currentComputation : Number ; 
7+ } 
8+ 
9+ class  Robot  { 
10+   constructor ( public  name : String )  { 
11+     this . name  =  name ; 
12+     this . currentComputation  =  0 ; 
13+   } 
14+ 
15+   // Given a mathematical operation, return a value based on the value passed, 
16+   // the operation and the number 10 
17+   compute ( operation ,  value )  { 
18+     let  computedValue  =  0 ; 
19+     switch ( operation )  { 
20+       case  '+' :
21+         computedValue  =  value  +  10 ; 
22+         break ; 
23+       case  '-' :
24+         computedValue  =  value  -  10 ; 
25+         break ; 
26+       case  '/' :
27+         computedValue  =  value  /  10 ; 
28+         break ; 
29+       case  '*' :
30+         computedValue  =  value  *  10 ; 
31+         break ; 
32+       default :
33+         console . log ( "Does not compute!!" ) 
34+     }  
35+     this . currentComputation  =  computedValue ; 
36+   } 
37+ 
38+   // Using an external JS module, format the computed value from our robot 
39+   displayCurrentComputation ( )  { 
40+     console . log ( Formatter . surroundWithStars ( this . currentComputation ) ) ; 
41+   } 
42+ } 
43+ 
44+ const  hal  =  new  Robot ( 'Hal' ) ; 
45+ hal . compute ( '+' ,  32 ) ; 
46+ hal . displayCurrentComputation ( ) ; 
Original file line number Diff line number Diff line change 1+ {
2+   "compilerOptions" : {
3+     "outDir" : " ./built" 
4+     "sourceMap" : true ,
5+     "allowJs" : true ,
6+     "target" : " es6" 
7+   },
8+   "include" : [
9+       " ./**/*" 
10+   ]
11+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments