typescript study
create-data: 2019-11-28 16:14:02author : yuyuanqiuversion : v0.0.1-alpha
handbook
basic types
booleannumber
- numbers are floating-point values
- support hex, decimal, binary and octal
string
- use double, single quotes
- use backtick/backquote for template strings, and embed expressions using
${ expr }
array
- using
[] - using generic array type like
Array<elemType>
tuple
- an array with a fixed number and known type of elements
- elements need not be same
- access an outside of indices fails with an error
let isDone: boolean = false;let decimal: number = 6;let hex: number = 0xf00d;let binary: number = 0b1010;let octal: number = 0o744;let color: string = "blue";color = 'red';let fullName: string = `Bob Bobbington`;let age: number = 37;let sentence: string = `Hello, my name is ${ fullName }.I'll be ${ age + 1 } years old next month.`;// equivalent like so:let sentence: string = "Hello, my name is " + fullName + ".\n\n" + "I'll be " + (age + 1) + " years old next month.";let list: number[] = [1, 2, 3];let list: Array<number> = [1, 2, 3];// declare a tuple typelet x: [string, number];// initialize itx = ["hello", 10]; // ok// initialize it incorrectlyx = [10, "hello"]; // errorconsole.log(x[0].substring(1)); // okconsole.log(x[1].substring(1)); // error, "number" does not have "substring"x[3] = "world"; // error, property "3" does not exist on type "[string, number]".console.log(x[5].toString()); // error, property "5" does not exist on type "[string, number]".