웹 개발에 주로 많이 사용되는 자바스크립트(JavaScript)를 기반으로 한 프로그래밍 언어
정적 타입(Type System) 지원, 더 안전하고 유지보수 용이한 코드 작성 가능
let age: number = 15; // age는 숫자
let name: string = "Alice"; // name은 문자열, Alice는 초기값
: 클래스와 인터페이스와 같은 개념을 통해 객체지향프로그래밍 구현
class Person {
constructor(public name: string, public age: number) {}
}
interface Animal { // 인터페이스(설명서)
name: string; // 속성
sound(): void; // 메소드
}
class Dog implements Animal { // Dog라는 클래스가
name: string;
constructor(name: string) {
this.name = name;
}
sound() {
console.log("Woof!"); // 인터페이스의 메소드를 구현
}
}
class Dog {
name: string; // 속성 선언
constructor(name: string) { // 생성자에서 name 매개변수 받기
this.name = name; // this.name: 객체의 name 속성에 매개변수 값 할당
}
}
const myDog = new Dog("Buddy"); // myDog라는 새로운 객체 생성
console.log(myDog.name); // "Buddy"