[TypeScript] TypeScript Grammar - 06~07

2023. 1. 8. 01:37·Development Study/Frontend
728x90

Previous Series

 

[TypeScript] TypeScript Grammar - 03~05

3. Type alias This Posting contains how to alias type to other variables type myType = number | string; let grade: myType = 3 Use type to create type and use to annotate another. Usually, we use it when we have to define two or more variables 3_01. Type al

time-map-installer.tistory.com


6.  Functions

This Posting contains how to make functinos in TypeScript


6_01.  Functions_return type

When contains void

function add(n1: number, n2: number):void{
	console.log(n1 + n2);
}

When contains return type

function add(n1: number, n2: number):void{
	return (n1 + n2);
}

function time(minute: number):boolean{
	return num = 60;
}

function functionTest (x: number): number{
	return x*3;
}

functionTest(2);
functionTest('10'); // not number value, error

6_02.  Functions_with optional

import { log } from "console";

function hello(name ?: string){
	return `hello, ${name || "world"}`;
}

const result = hello();
const result2 = hello("Sam Rider");

log(result); // hello, world
log(result2); // hello, Sam Rider

I think that part of "${name || 'world'}" may be show 'world' when the name's type is different from the setting


7.  Loop

Maybe you are familiar with this, because in Python, there is a 'for loop' and it is similar


7_01.  Loop_let ... in

array

import { log } from 'console';

let list: string[] = ['test1', 'test2', 'test3'];

for (let i in list){
	log(i, list[i]);
}

Q. Can we do the type designation on the following list?

 

import { log } from 'console';

let animals: any = {a: 'rat', b: 'cat', c: 'tiger'}
for (let anim in animals){
	log(anim, animals[anim]);
}

A. Of course No


7_02.  Loop_let ... of

import { log } from 'console';

let str: string = "hello";

for (const a of str){
	log(a);
}

End

 

 

Next Series

 

[TypeScript] TypeScript Grammar - 08

Previous Series [TypeScript] TypeScript Grammar - 02 Previous Series [TypeScript] TypeScript Grammar - 01 1. Type Annotation TypeScript uses colon ( : ) behind variables to annotate a type const myMaxHamburger: number = 3; const answer: string = "Yes I can

time-map-installer.tistory.com

 

728x90
저작자표시 비영리 변경금지 (새창열림)

'Development Study > Frontend' 카테고리의 다른 글

[TypeScript] TypeScript Grammar - 09  (0) 2023.01.08
[TypeScript] TypeScript Grammar - 08  (0) 2023.01.08
[TypeScript] TypeScript Grammar - 03~05  (0) 2023.01.07
[TypeScript] TypeScript Grammar - 02  (0) 2023.01.07
[TypeScript] TypeScript Grammar - 01  (0) 2023.01.07
'Development Study/Frontend' 카테고리의 다른 글
  • [TypeScript] TypeScript Grammar - 09
  • [TypeScript] TypeScript Grammar - 08
  • [TypeScript] TypeScript Grammar - 03~05
  • [TypeScript] TypeScript Grammar - 02
ThreeLight
ThreeLight
ThreeLight Studio의 블로그, TimeMap.exe에 오신 것을 환영합니다.
  • ThreeLight
    TimeMap.exe
    ThreeLight
  • 전체
    오늘
    어제
    • 분류 전체보기 (245)
      • Checkpoint (1)
      • (3D)Dev Deep Dive (0)
        • Templates & Guides (9)
        • Frontend origin (9)
        • Backend origin (1)
        • TroubleShootings (4)
      • Development Study (95)
        • Frontend (36)
        • Backend (21)
        • CS(Computer Science) (2)
        • Background Knowledges (11)
        • Algorithm (2)
        • Mobile (3)
        • AWS (6)
        • Python (6)
        • MSW(MapleStoryWorlds) (8)
      • Coding Test (59)
        • 문제.zip (1)
        • BaekJoon_JavaScript (0)
        • Programmers_JavaScript (9)
        • BaekJoon_Python (23)
        • Programmers_Python (10)
        • Undefined_Python (3)
        • Programmers_SQL (13)
      • 활동내역.zip (43)
        • 개인 (21)
        • Techeer (12)
        • Bootcamp (7)
        • Hackathon (1)
        • TeamProjects (2)
      • 여기 괜찮네??(사이트 | App) (5)
      • 재미있는 주제들 (8)
      • 개발 외 공부 저장소 (11)
        • 생산운영관리 (3)
        • 생활속의금융 (6)
        • 경영정보시스템 (2)
  • 링크

    • TimeMap.dmg (Portfolio)
    • GitHub 바로가기
    • 오픈프로필(카카오톡)
    • Medium 바로가기
    • Disquiet 바로가기
    • LinkedIn 바로가기
  • 인기 글

  • 태그

    HTML
    programmers
    프로그래머스
    JavaScript
    CSS
    Baek Joon
    Python
    SQL
    TypeScript
    react
  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.1
ThreeLight
[TypeScript] TypeScript Grammar - 06~07
상단으로

티스토리툴바