Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coding devil/8: Utility Types #11

Open
wants to merge 14 commits into
base: recently
Choose a base branch
from
7 changes: 7 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### โœ // lecture/num : task

### ๐Ÿ“Description
// Description ์ž‘์„ฑ

### ๐Ÿ“ŒSummary
// Summary ์ž‘์„ฑ
26 changes: 26 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function merge(objA, objB) {
return Object.assign({}, objA, objB);
}
var mergeObj = merge({ name: 'Max', hobbies: ['Sports'] }, { age: 30 });
// const mergeObj2 = merge({ name: 'Max' }, { age: 30 });
console.log(mergeObj.name);
function countAndDescribe(element) {
var 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 ๋ฐœ์ƒ : ํƒ€์ž…์„ ๋ช…์‹œํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๋‹ค.
}
console.log(countAndDescribe('Hi, there!'));
//๊ฐ์ฒด key, value์— ํ•ฉ๋‹นํ•œ ts Generics
function extractAndConvert(obj, key) {
return 'Value: ' + obj[key];
}
// T : { name: 'Max' }, U : 'name'
var value = extractAndConvert({ name: 'Max' }, 'name');
console.log(value);
// ์œ„์˜ ์„ค๋ช…์ด ๋‹ค์†Œ ๋ณต์žกํ•˜๋‹ค. ์šฐ์„  U๋Š” ๊ฐ์ฒด์˜ key, value ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  U๋Š” T์—์„œ key์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ์ƒ์†๋ฐ›์•„ keyํƒ€์ž…์„ ์ง€๋‹ˆ๊ณ  ์žˆ๋‹ค. ์ฆ‰, name ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ˜•ํƒœ์ด๋‹ค. return ๊ฐ’์˜ ๋ฐ˜ํ™˜์„ ๋ณด๋ฉด object์˜ ํ•ด๋‹น key๊ฐ’์„ ๋„์ถœํ•˜๋ ค๋Š” ์˜๋„์ด๋‹ค. ๋”ฐ๋ผ์„œ, console.log(value)์˜ ๊ฐ’์€ Max๊ฐ€ ๋œ๋‹ค.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// src/index.ts
const message: string = "Hello, TypeScript!";
console.log(message);
// NonNullable<Type>
// Null๊ณผ undefind๋ฅผ ์ œ์™ธํ•œ ํƒ€์ž… ์ƒ์„ฑ

type T1 = string | null | undefined | void;
type T2 = NonNullable<T1>;
12 changes: 11 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"compilerOptions": {
"target": "es6", // ์ปดํŒŒ์ผ ์‹œ ์‚ฌ์šฉํ•  ECMAScript ๋ฒ„์ „
"target": "es6", // ์ปดํŒŒ์ผ ์‹œ ์‚ฌ์šฉํ•  ECMAScript ๋ฒ„์ „
"lib" : [
"dom",
"dom.iterable",
"esnext",
],
"allowJs": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "commonjs", // ๋ชจ๋“ˆ ์‹œ์Šคํ…œ
"strict": true, // ์—„๊ฒฉํ•œ ํƒ€์ž… ๊ฒ€์‚ฌ
"esModuleInterop": true, // ES ๋ชจ๋“ˆ๊ณผ์˜ ์ƒํ˜ธ ์šด์šฉ์„ฑ ์„ค์ •
Expand Down