diff --git a/dist/app.js b/dist/app.js index fa5b1c1..38e7fe9 100644 --- a/dist/app.js +++ b/dist/app.js @@ -1,31 +1,25 @@ "use strict"; -var Department = /** @class */ (function () { - // readonly는 프로퍼티를 초기화한 후 수정할 수 없다. 즉, 한번 할당 되면 변경되면 안되는 고유 번호들을 설정할 때 readonly를 사용한다. - function Department(id, name) { - this.id = id; - this.name = name; - // private id: string; - // private name: string; - this.employees = []; - // this.id = id; - // this.name = n +// function merge(objA: T, objB: U): T & U { +// return Object.assign({}, objA, objB); +// } +function countAndDescribe(element) { + let descriptionText = "Got no Value"; + if (element.length === 1) { + descriptionText = "Got 1 element"; } - Department.prototype.describe = function () { - console.log("Department (".concat(this.id, "): ").concat(this.name)); - }; - Department.prototype.addEmployee = function (employee) { - // this.id = '2'; // readonly이기 때문에 error가 발생한다. - this.employees.push(employee); - }; - Department.prototype.printEmployeeInformation = function () { - console.log(this.employees.length); - console.log(this.employees); - }; - return Department; -}()); -var accounting = new Department('1', 'Accounting'); -accounting.addEmployee('Max'); -accounting.addEmployee('Manu'); -// accounting.employees[2] = 'Anna'; -accounting.describe(); -accounting.printEmployeeInformation(); + else if (element.length > 1) { + descriptionText = 'Got ' + element.length + ' elements.'; + } + return [element, descriptionText]; + // return ['Max', 'Tom']; //error 발생 : 타입을 명시했기 때문에 그렇다. +} +console.log(countAndDescribe('Hi, there!')); +//객체 key, value에 합당한 ts Generics +function extractAndConvert(obj, key) { + return 'Value: ' + obj[key]; +} +// T : { name: 'Max' }, U : 'name' +const value = extractAndConvert({ name: 'Max', age: 30 }, 'age'); //age를 추가한 에시 +console.log(value); +// 위의 설명이 다소 복잡하다. 우선 U는 객체의 key, value 형태로 구성되어 있다. 그리고 U는 T에서 key에 해당되는 값을 상속받아 key타입을 지니고 있다. 즉, name 부분을 반환하는 형태이다. return 값의 반환을 보면 object의 해당 key값을 도출하려는 의도이다. 따라서, console.log(value)의 값은 Max가 된다. +// 이를 조금더 변형해보자면 object에 age를 추가하면 key: U타입에 age를 입력할 수 있게 된다. diff --git a/index.html b/index.html new file mode 100644 index 0000000..e2da7ca --- /dev/null +++ b/index.html @@ -0,0 +1,14 @@ + + + + + + + Document + + + + + + + \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index d8a50ee..a063cd9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,83 +1,42 @@ -class Department { - // private id: string; - // private name: string; - private employees: string[] = []; - // readonly는 프로퍼티를 초기화한 후 수정할 수 없다. 즉, 한번 할당 되면 변경되면 안되는 고유 번호들을 설정할 때 readonly를 사용한다. - constructor(private readonly id: string, public name: string) { - // this.id = id; - // this.name = n - } - describe(this: Department) { - console.log(`Department (${this.id}): ${this.name}`); - } - - addEmployee(employee: string) { - // this.id = '2'; // readonly이기 때문에 error가 발생한다. - this.employees.push(employee); - } - - printEmployeeInformation() { - console.log(this.employees.length); - console.log(this.employees); - } -} +// function merge(objA: T, objB: U): T & U { +// return Object.assign({}, objA, objB); +// } -class ITDepartment extends Department { - admins: string[]; - constructor(id: string, admins: string[]) { - super(id, 'IT'); - this.admins = admins; - } -} +// const mergeObj = merge({ name: 'Max', hobbies: ['Sports'] }, { age: 30 }); -class AccountingDepartment extends Department { - private lastReport: string; +// const mergeObj2 = merge({ name: 'Max' }, { age: 30 }); - get mostRecentReport() { - if (this.lastReport) { - return this.lastReport; - } - throw new Error('No report found.'); - } +// console.log(mergeObj.name); - set setMostRecentReport(value: string) { - if (!value) { - throw new Error('Please pass in a valid value!') - } - this.addReport(value); - this.lastReport = value; // 여기서 lastReport를 업데이트 그래야 lastReport가 비어있지 않기 때문에 정상적으로 동작을 한다. - } - - constructor(id: string, private reports: string[]) { - super(id, 'Account'); - //strictPropertyInitialization 활성화로 초기화 해줘야 함. - this.lastReport = reports[0] || ""; // 초기값을 할당 (reports가 비어있으면 빈 문자열) - } - - addReport(text: string) { - this.reports.push(text); - } +//interface와 Generics의 조합 +interface Length { + length: number; +} - printReports() { - console.log(this.reports); +function countAndDescribe(element: T): [T, string] { //반환값은 Generics를 사용했기에 타입추론이되지만 명확하게 명시를 해줘야한다면 현재와같이 작성하면 된다. + let descriptionText = "Got no Value"; + if (element.length === 1) { + descriptionText = "Got 1 element"; + } else if (element.length > 1) { + descriptionText = 'Got ' + element.length + ' elements.'; } + return [element, descriptionText]; + // return ['Max', 'Tom']; //error 발생 : 타입을 명시했기 때문에 그렇다. } -const accounting = new Department('1', 'Accounting'); -const ITaccounting = new ITDepartment('2', ['Max']); - -ITaccounting.addEmployee('Max'); -ITaccounting.addEmployee('Manu'); -// accounting.employees[2] = 'Anna'; -ITaccounting.describe(); -ITaccounting.printEmployeeInformation(); +console.log(countAndDescribe('Hi, there!')); -const NewAccounting = new AccountingDepartment('d2', []); - -// console.log(NewAccounting.mostRecentReport); //report가 추가되지 않아서 Error -NewAccounting.setMostRecentReport = 'Year End Report'; -NewAccounting.addReport('Something went wrong...'); +//객체 key, value에 합당한 ts Generics +function extractAndConvert( + obj: T, + key: U +) { + return 'Value: ' + obj[key]; +} +// T : { name: 'Max' }, U : 'name' +const value = extractAndConvert({ name: 'Max', age: 30 }, 'age'); //age를 추가한 에시 +console.log(value); -console.log(NewAccounting.mostRecentReport); //report가 있어서 문제없이 출력 +// 위의 설명이 다소 복잡하다. 우선 U는 객체의 key, value 형태로 구성되어 있다. 그리고 U는 T에서 key에 해당되는 값을 상속받아 key타입을 지니고 있다. 즉, name 부분을 반환하는 형태이다. return 값의 반환을 보면 object의 해당 key값을 도출하려는 의도이다. 따라서, console.log(value)의 값은 Max가 된다. -NewAccounting.printReports(); +// 이를 조금더 변형해보자면 object에 age를 추가하면 key: U타입에 age를 입력할 수 있게 된다. \ No newline at end of file