1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
interface User { type: 'user'; name: string; age: number; occupation: string; }
interface Admin { type: 'admin'; name: string; age: number; role: string; }
function logUser(user: User) { const pos = users.indexOf(user) + 1; console.log(` - #${pos} User: ${user.name}, ${user.age}, ${user.occupation}`); }
function logAdmin(admin: Admin) { const pos = admins.indexOf(admin) + 1; console.log(` - #${pos} Admin: ${admin.name}, ${admin.age}, ${admin.role}`); }
const admins: Admin[] = [ { type: 'admin', name: 'Will Bruces', age: 30, role: 'Overseer' }, { type: 'admin', name: 'Steve', age: 40, role: 'Steve' } ];
const users: User[] = [ { type: 'user', name: 'Moses', age: 70, occupation: 'Desert guide' }, { type: 'user', name: 'Superman', age: 28, occupation: 'Ordinary person' } ];
export function swap<T,R>(v1:T, v2:R):[R,T] { return [v2, v1]; }
function test1() { console.log('test1:'); const [secondUser, firstAdmin] = swap(admins[0], users[1]); logUser(secondUser); logAdmin(firstAdmin); }
function test2() { console.log('test2:'); const [secondAdmin, firstUser] = swap(users[0], admins[1]); logAdmin(secondAdmin); logUser(firstUser); }
function test3() { console.log('test3:'); const [secondUser, firstUser] = swap(users[0], users[1]); logUser(secondUser); logUser(firstUser); }
function test4() { console.log('test4:'); const [firstAdmin, secondAdmin] = swap(admins[1], admins[0]); logAdmin(firstAdmin); logAdmin(secondAdmin); }
function test5() { console.log('test5:'); const [stringValue, numericValue] = swap(123, 'Hello World'); console.log(` - String: ${stringValue}`); console.log(` - Numeric: ${numericValue}`); }
[test1, test2, test3, test4, test5].forEach((test) => test());
|