Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion 1week/assignment/01.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* 아래의 객체들을 동일하게 선언할 수 있는 타입을 작성해주세요.
*/
type Person = {};

type Name = string | { first: number, last: string}
type EventDate = string | Date | null
Comment on lines +5 to +6

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 유니온이 많거나 객체 형태의 타입은 타입 별칭으로 분리해주는 게 좋죠! 멋있어요😊


type Person = {
name: Name;
age: string | number;
sex: string;
birth: EventDate;
death?: EventDate;
};

const personA: Person = {
name: 'Alan Turing',
Expand Down
27 changes: 26 additions & 1 deletion 1week/assignment/02.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ const palette = {
white: '#FFFFFF',
} as const;



const basicColors = {
black: '#000000',
white: '#FFFFFF',
Expand All @@ -163,4 +165,27 @@ const basicColors = {
navy: '#000080',
};

const YOURE_PALETTE_THEME = {};
type ValueOf<T> = T[keyof T];
Comment on lines 167 to +168

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮 오!! 이런 유틸리티 타입을 만드셔서 아래의 상수를 작성해주셨네요! 잘 봤습니다👏


type PaletteTheme = {
[key in keyof typeof basicColors]: ValueOf<typeof palette>;
}

const YOURE_PALETTE_THEME: PaletteTheme = {
black: '#00008B',
white: '#006400',
red: '#FFFAF0',
lime: '#00FF00',
blue: '#0000FF',
yellow: '#FFFF00',
'cyan/aqua': '#483D8B',
'magenta/fuchsia': '#FF00FF',
silver: '#C0C0C0',
gray: '#808080',
maroon: '#800000',
olive: '#F0FFF0',
green: '#008000',
purple: '#FFE4E1',
teal:'#F0E68C',
navy: '#F08080',
};
5 changes: 4 additions & 1 deletion 1week/assignment/03.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/**
* 아래의 userRole이 가진 리터럴 타입을 제거하여 UserInformation을 유연하게 만드려면 어떻게 할까요?
*/

type UserRole = 'normal' | 'vip' | 'admin'

type UserInformation = {
userId: string;
userName: string;
userRole: 'normal' | 'vip' | 'admin';
userRole: UserRole;
password: string;
Comment on lines 9 to 11

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리터럴 타입을 타입 별칭으로 분리!! 아주 좋습니다!
userRole이 유연해지려면.... 제너릭을 활용하면 어떨까요?

};
14 changes: 11 additions & 3 deletions 1week/assignment/04.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
* - 특정 index의 원소를 변경하는 update 메서드를 만들어주세요.
*/

class List {
private items: any[] = [];
class List<T> {
private items: T[] = [];
Comment on lines +8 to +9

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


add(item: any) {
add(item: T) {
this.items.push(item);
}

get(index: number) {
return this.items[index];
}

remove(index: number) {
return this.items.splice(index, 1);
}
Comment on lines +19 to +21

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제거된 값을 return 해주셨네요! 좋습니다🙌


update(index: number, value: T) {
return this.items[index] = value;
}
}
6 changes: 5 additions & 1 deletion 1week/assignment/05.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ type HumanProps = {
};
};

const Bob: HumanProps = {
type DeepReadonly<T> = {
readonly [key in keyof T]: DeepReadonly<T[key]>
}
Comment on lines +19 to +21

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인덱스 시그니처의 좋은 활용 예죠! 아주 좋습니다🙌
저도 리뷰를 받은 부분인데, 추후에는 고도화를 할 수 있는 부분이 많아요.
null, undefined 같은 타입이 들어오면 readonly가 아니게 되거든요!


const Bob: DeepReadonly<HumanProps> = {
firstName: 'Bob',
surname: 'Keel',
profile: {
Expand Down
16 changes: 6 additions & 10 deletions 1week/assignment/06.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@
* 아래의 코드 중 타입 별칭을 제거하여 동일한 결과를 내도록 작성해주세요.
*/

type SavingAction = {
type: 'saving';
payload: string[];
type ActionType = 'saved' | 'saving'
type Action = {
type: ActionType,
payload?: string[]
};

const savingAction: SavingAction = {
const savingAction: Action = {
type: 'saving',
payload: ['Apple', 'Banana', 'Strawberry'],
};

type SavedAction = {
type: 'saved';
};

const savedAction: SavedAction = {
const savedAction: Action = {
Comment on lines +11 to +16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

타입을 사전에 정의해서 상수를 제한했네요! 좋습니다 ㅎㅎ
문제 의도와는 달랐지만 타입을 정의하는 형태가 보기 좋아요!

type: 'saved',
};

type Actions = SavingAction | SavedAction;
6 changes: 3 additions & 3 deletions 1week/assignment/07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* 아래의 코드 중 Cylinder 타입을 유지하기 위한 코드를 작성해주세요.
*/

interface Cylinder {
radius: number;
height: number;
class Cylinder {
radius = 1;
height = 1;
}
Comment on lines +5 to 8

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 코드는 아래와 같이 단축된답니다!

class Cylinder {
  consturctor(public radius:number, public height:number){}
}


function calculateVolumne(shape: unknown) {
Expand Down
81 changes: 81 additions & 0 deletions 1week/assignment/08.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* 아래의 함수가 동작하도록 리팩토링 해주세요.
* - 문자열 배열을 입력하면 2차원 배열로 각 아이템이 p 태그로 묶여서 나옵니다.
* [
[
'<p> 모두 잠드는 밤에 </p>',
'<p> 혼자 우두커니 앉아 </p>',
'<p> 다 지나버린 오늘을 </p>',
'<p> 보내지 못하고서 깨어있어 </p>',
'<p> 누굴 기다리나 </p>',
'<p> 아직 할 일이 남아 있었던가 </p>',
'<p> 그것도 아니면 돌아가고 싶은 </p>',
'<p> 그리운 자리를 떠올리나 </p>',
'<p> 무릎을 베고 누우면 나 아주 어릴 적 </p>',
'<p> 그랬던 것처럼 머리칼을 넘겨줘요 </p>',
'<p> 그 좋은 손길에 까무룩 잠이 들어도 </p>',
'<p> 잠시만 그대로 두어요 </p>',
'<p> 깨우지 말아요 아주 깊은 잠을 잘 거예요 </p>',
'<p> 조용하던 두 눈을 </p>',
'<p> 다시 나에게 내리면 </p>',
'<p> 나 그때처럼 말갛게 </p>',
'<p> 웃어 보일 수 있을까 </p>',
'<p> 나 지친 것 같아 </p>',
'<p> 이 정도면 오래 버틴 것 같아 </p>',
'<p> 그대 있는 곳에 돌아갈 수 있는 </p>',
'<p> 지름길이 있다면 좋겠어 </p>',
'<p> 무릎을 베고 누우면 나 아주 어릴 적 </p>',
'<p> 그랬던 것처럼 머리칼을 넘겨줘요 </p>',
'<p> 그 좋은 손길에 까무룩 잠이 들어도 </p>',
'<p> 잠시만 그대로 두어요 </p>',
'<p> 깨우지 말아요 아주 깊은 잠을 잘 거예요 </p>',
'<p> 스르르르륵 스르르 깊은 잠을 잘 거예요 </p>',
'<p> 스르르르륵 스르르 깊은 잠을 </p>'
]
]
* - 함수를 리팩토링해주세요.
*/
function parseTaggedText(lines) {
var paragraphs = [];
var currPara = [];
var addParagraph = function (text) {
currPara.push("<p> " + text + " </p>");
};
for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
var line = lines_1[_i];
addParagraph(line);
}
paragraphs.push(currPara);
return paragraphs;
}
var parameter = [
'모두 잠드는 밤에',
'혼자 우두커니 앉아',
'다 지나버린 오늘을',
'보내지 못하고서 깨어있어',
'누굴 기다리나',
'아직 할 일이 남아 있었던가',
'그것도 아니면 돌아가고 싶은',
'그리운 자리를 떠올리나',
'무릎을 베고 누우면 나 아주 어릴 적',
'그랬던 것처럼 머리칼을 넘겨줘요',
'그 좋은 손길에 까무룩 잠이 들어도',
'잠시만 그대로 두어요',
'깨우지 말아요 아주 깊은 잠을 잘 거예요',
'조용하던 두 눈을',
'다시 나에게 내리면',
'나 그때처럼 말갛게',
'웃어 보일 수 있을까',
'나 지친 것 같아',
'이 정도면 오래 버틴 것 같아',
'그대 있는 곳에 돌아갈 수 있는',
'지름길이 있다면 좋겠어',
'무릎을 베고 누우면 나 아주 어릴 적',
'그랬던 것처럼 머리칼을 넘겨줘요',
'그 좋은 손길에 까무룩 잠이 들어도',
'잠시만 그대로 두어요',
'깨우지 말아요 아주 깊은 잠을 잘 거예요',
'스르르르륵 스르르 깊은 잠을 잘 거예요',
'스르르르륵 스르르 깊은 잠을',
];
console.log(parseTaggedText(parameter));
15 changes: 4 additions & 11 deletions 1week/assignment/08.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,15 @@ function parseTaggedText(lines: string[]): string[][] {
const paragraphs: string[][] = [];
const currPara: string[] = [];

const addParagraph = () => {
if (currPara.length) {
paragraphs.push(currPara);
currPara.length = 0; // Clear the lines
}
const addParagraph = (text: string) => {
currPara.push(`<p> ${text} </p>`)
};

for (const line of lines) {
if (!line) {
addParagraph();
} else {
currPara.push(`<p> ${line} </p>`);
}
addParagraph(line);
}

addParagraph();
paragraphs.push(currPara)
return paragraphs;
}

Expand Down
8 changes: 7 additions & 1 deletion 1week/assignment/09.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
* 아래의 함수가 정상적으로 동작하기 위해 어떤 타입을 활용하면 될까요?
*/

function prettyPrint(x: any): string {
type Parameter = string | number | {
x: Parameter;
length: number;
mop: (callback: (x: Parameter)=> string) => string[]
}

function prettyPrint(x: Parameter): string {
Comment on lines +5 to +11

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오....! 타입을 이렇게 프로퍼티로 제한해주셨네요! 정말 신선합니다ㅎㅎ
잘 보고 가요! 다만 객체나 function 타입 등이 들어오면... 어떻게 될까요? 😳

if (typeof x === 'string') return `"${x}"`;

if (typeof x === 'number') return String(x);
Expand Down
13 changes: 13 additions & 0 deletions 1week/assignment/10.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* never, unknown, any 타입에 대해 정리하여 최대한 상세하게 적어주세요.
*/

const never = `
TS에서 가질 수 있는 가장 작은 집합
`;

const unknown = `
모든 타입을 할당 받을 수 있으나 any와 다른 것은 unknown 타입으로 선언된 변수는 any를 제외한 다른 타입으로 선언된 변수에 할당될 수 없다는 것
`

const any = `
모든 타입을 할당 받을 수 있는 TS의 치트키.
하지만 JS프로젝트를 TS로 마이그레이션 할때만 사용하도록 하고 실제 협업에서는 사용을 지양하자.
`
Comment on lines +5 to +16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

백틱으로 정리!! 이것도 새로운 코드라 재밌었습니다🙌