Type Scriptの型(基本)
let hasValue = true;
let count = 10;
let float = 3.14;
let negative = -0.12;
let single = 'hello';
let double = "hello";
let back = `hello`;
let hello: string;
hello = 'hello';
const person: {
name: string
age: number
} = {
name: 'jack',
age: 21
}
const fruits: string[] = ['Apple', 'Banana', 'Grape'];
const book: [string, number, boolean] = ['business', 1500, false];
enum CoffeeSize {
SHORT,
TALL = 'TALL',
GRANDE = 1,
VENTI
}
const coffee = {
hot: true,
size: CoffeeSize.TALL
}
coffee.size = CoffeeSize.SHORT;
let anything: any = true;
anything = 'hello';
anything = ['hello', 33, true];
anything = {};
anything.fjiafjaj = 'faijfi';
let banana = 'banana';
banana = anything;
let unionType: number | string = 10;
let unionTypes: (number | string)[] = [21, 'hello']
type ClothSize = 'small' | 'medium' | 'large';
const apple = 'apple';
let clothSize: ClothSize = 'large';
const cloth: {
color: string;
size: ClothSize
} = {
color: 'white',
size: 'medium'
}
function add(num1: number, num2: number): number {
return num1 + num2
}
function sayHello(): void {
}
let tmp: undefined;
const anotherAdd: (n1: number, n2: number) => number = function (num1, num2) {
return num1 + num2
};
const doubleNumber: (num: number) => number = num => num * 2;
function doubleAndHandle(num: number, cb: (num: number) => void): void {
const doubleNum = cb(num * 2);
}
doubleAndHandle(21, doubleNum => {
return doubleNum
});
let unknownInput: unknown;
let anyInput: any;
let text: string;
unknownInput = 'hello';
unknownInput = 21;
unknownInput = true;
text = anyInput;
if (typeof unknownInput === 'string') {
text = unknownInput;
}
function error(message: string) {
throw new Error(message);
}
console.log(error('This is an error'));