File tree 2 files changed +58
-0
lines changed
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ //TS exmples. Visit localhost:3000/playground to see the slogs
2
+ const main = ( log :any ) :void => {
3
+
4
+ type level = 'shitty' | 'superPlayer' | 'god' ;
5
+
6
+ interface Player {
7
+ name :string ,
8
+ shirtnumber : number
9
+ age : number ,
10
+ level : level
11
+ }
12
+ type PlayerPreveiw = Pick < Player , "name" | "level" > ; //define a new type with name and level properties
13
+
14
+
15
+
16
+ const toString = ( player : Partial < Player > ) => {
17
+ return player . name + ' - ' + player . shirtnumber ;
18
+ }
19
+ const printPartialPlayer = ( p : Partial < Player > ) => {
20
+ log ( p . name + ', age: ' + p . age )
21
+ }
22
+
23
+ const player1 :Partial < Player > = { name :'Messi' , level :'god' , shirtnumber :10 } ; //Partial utility types if only parts of the object are required
24
+ const prevPlayer : PlayerPreveiw = { name :"Xavi" , level :'god' } ;
25
+ printPartialPlayer ( { name : 'Test' , age :1 } ) ;
26
+ log ( toString ( player1 ) ) ;
27
+
28
+
29
+ }
30
+ export { main }
Original file line number Diff line number Diff line change
1
+ import type { NextPage } from "next" ;
2
+ import { useEffect , useState } from "react" ;
3
+ import styles from "../styles/Home.module.css" ;
4
+ import { main } from "../TSexamples/Main" ;
5
+
6
+ const Playground : NextPage = ( ) => {
7
+ const [ logs , setLogs ] = useState < string [ ] > ( [ ] ) ;
8
+
9
+
10
+ let logItem : Function ; //Function type
11
+ logItem = ( item : any ) : void => {
12
+ const newAr : string [ ] = [ ...logs , JSON . stringify ( item ) ] ;
13
+ setLogs ( newAr ) ;
14
+ } ;
15
+ useEffect ( ( ) => {
16
+ main ( logItem ) ;
17
+ } , [ 0 ] )
18
+
19
+ return (
20
+ < div className = { styles . container } >
21
+ < h1 > Det nye-nye-bibliotek.dk - WorkPage </ h1 >
22
+ < button onClick = { ( ) => setLogs ( [ ] ) } > Clear</ button >
23
+ { logs . map ( l => < p > { l } </ p > ) }
24
+ </ div >
25
+ ) ;
26
+ } ;
27
+
28
+ export default Playground ;
You can’t perform that action at this time.
0 commit comments