定义变量类型
const num: number = 1
定义函数参数类型
const init: (p: str) => void
= function(param) {
alert(param)
}
定义外部变量
declare var document
document.str = 'hello'
原始类型
var age: number = 2
var rating = 98.25
var isReady: boolean = true
var hasData = true
var firstName: string = 'John'
var lastName = 'Papa'
Arrays and Indexers
var names: string[] = ['John', 'Dan', 'Aaron', 'Fritz']
var firstPerson: string
firstPerson = names[0]
Null and Undefined
var num: number = null
var str: string = null
var isHappy: boolean = null
var customer: {} = null
var age: number;
var customer = undefined
var quantity: number
var company = undefined
对象类型
// object
var square = { h: 10, w: 20 }
var points: Object = { x: 10, y: 20 }
// function
var multiply = function (x: number) {
return x * x
}
var multiplyMore: Function;
multiplyMore = function (x: number) {
return x * *
}
Functions
image
var myFunc = function (h: number, w:number) {
return h * w
}
var myFunc = (h: number, w: number) => h * w
// Void (Used as the return type for functions that return no value)
var greetMe : (msg: string) => void
greetMe = function (msg) {
console.log(msg)
}
greetMe('Hello!')
Function Interface
interface SquareFunction {
// 接收一个number类型参数,返回一个number类型的值
(x: number): number
}
var squareItBasic: SquareFunction = num => num * num
interface Rectangle {
h: number
w?: number
}
squareIt: (react: Rectangle) => number
|
请发表评论