第一部 变量,never,元组,可选链,联合等
var name:string = "zlk"
var age:number = 18
var num1:number = 222
var num2:number = 0x100
var num3:number = 0o100
console.log(name,age,num1,num2,num3)
var list:string[] = []
list.push('abc')
list.push('def')
console.log(list)
var bool1 = false
var bool2 = 20 > 22
console.log(bool1,bool2)
var obj = {
name:'zlk',
age:123
}
console.log(obj.name,obj.age)
var txt:string = "str"
var num = 18
console.log(txt,num)
var title1 = Symbol('title')
var title2 = Symbol("title")
const info = {
[title1]:'程序员',
[title2]:'老师'
}
console.log(info[title2])
var antest:any = 123
function foo(){
return 'abc'
}
function bee(){
return 123
}
var flag = true
var feast:unknown
if (flag){
feast = foo()
}else {
feast = bee()
}
function never(state:string|number|boolean){
switch (typeof state){
case 'string':{
console.log('string处理')
break
}
case 'number':{
console.log('number处理')
break
}
case 'boolean':{
console.log('boolean处理')
break
}
default:{
const nem:never = state
}
}
}
never(123)
never('abc')
never(true)
const list5 :[string,number,boolean] = ['abc',123,true]
const show1 = list5[0]
const show2 = list5[1]
const show3 = list5[2]
function usestate<T>(state:T){
let content = state;
const usecont = (newstate:T) => {
content = newstate
}
const str :[T,(newstate:T) => void] = [content,usecont]
return str
}
const [one,two] = usestate(12)
two(10)
const [title,settitle] = usestate("abc")
const [haha,setflag] = usestate(true)
function abc(message:string){
return message
}
var list6 = ['abc','bnm','jkl']
list6.forEach((item) => {
item.split(" ")
})
function print(point:{x:number,y:number,z?:number}){
console.log("x坐标",point.x)
console.log("y坐标",point.y)
console.log("z坐标",point.z)
}
print({x:1,y:2})
print({x:1,y:2,z:3})
function cdprint(a:number|string){
console.log(a)
}
cdprint(123)
cdprint("abcd")
type IDaios = number|string
function printID(id:IDaios){
if (typeof id === 'string'){
console.log("你的id是:",id.toUpperCase())
}else {
console.log("你的id是",id)
}
}
printID('a')
printID(1)
type Person = {
name:string,
friend?:{
name:string,
age?:number,
grilfriend?:{
name:string,
age:number
}
}
}
const shenye: Person = {
name:'zlk',
friend:{
name:'kobe'
}
}
console.log(shenye.name)
console.log(shenye.friend?.age)
console.log(shenye.friend?.grilfriend?.name)
export {}
const zlkya = function (message?:string){
console.log(message!.toUpperCase())
}
zlkya('zlk')
const abcd = 'hello world'
console.log(!!abcd)
let hanfei:string|null = null
let relust = hanfei ?? 'nihao'
console.log(relust)
const nihao1:'hello' = 'hello'
type zimian = 'left'|'right'|'bottom'
const zimianfun = function (zm:zimian){
console.log(zm)
}
zimianfun('left')
第二部分 函数
const typed = function (num:number|string){
if (typeof num === "number"){
return num * 2
}
if (typeof num === "string"){
console.log(num)
}
}
const a = typed(2)
console.log(a)
typed("abc")
const obj = {
name:"zlk"
}
class Date{
friend(){
console.log('shili')
}
}
let shili = new Date()
const typed1 = function (date:Date|string){
if (date instanceof Date){
console.log(date.friend())
}else {
console.log(date)
}
}
typed1(obj.name)
typed1(shili)
type fish = {swim:() => void}
type Dog = {run:() => void}
function move(animal:fish|Dog){
if ('swim' in animal){
animal.swim()
}else {
animal.run()
}
}
type CalcFun = (num1:number,num2:number) => number
function call(fn:CalcFun){
console.log(fn(20,30))
}
function sum(num1:number,num2:number){
return num1 * num2
}
function mul(num1:number,num2:number){
return num1 + num2
}
call(sum)
call(mul)
function abcd1(num1:string,num2:string):string
function abcd1(num1:number,num2:number):number
function abcd1(num1:any,num2:any):any{
return num1+num2
}
console.log(abcd1(20,20))
console.log(abcd1("hah","lala"))
function suma(num:string|any[]){
return num.length
}
function sumb(num:string):string
function sumb(num:any[]):any[]
function sumb(num:any):any{
return num.length
}
console.log(suma(['a','b','c','d']))
console.log(sumb('hhhhhhhh'))
export {}
第三步 类,类继承
class person {
name:string
age:number
constructor(name:string,age:number){
this.name = name
this.age = age
}
run(){
return this.name + 'person'
}
}
let p = new person('zlk',18)
console.log(p.name)
console.log(p.age)
console.log(p.run())
class friend{
name:string = ''
age:number = 0
constructor(name:string,age:number) {
this.name = name
this.age = age
}
run(){
return this.name.toUpperCase()
}
}
class xiaoming extends friend{
son:number
constructor(name:string,age:number,son:number) {
super(name,age);
this.son = son
}
}
class xiaohong extends friend{
son:number
constructor(name:string,age:number,son:number) {
super(name,age);
this.son = son
}
}
let pa = new xiaoming('xiaoming',16,18)
let pa2 = new xiaohong('xiaohong',26,28)
console.log(pa.name)
console.log(pa.run())
console.log(pa.son)
console.log(pa.age)
console.log(pa2.name)
console.log(pa2.son)
class sonan{
readonly name:string = 'hong'
}
const p3 = new sonan()
console.log(p3.name)
export {}
第四部 接口,泛型与枚举
interface IPoint {
x:number
y:number
friend?:{
name:string
age?:number
readonly rie:string
}
}
const obj:IPoint = {
x:1,
y:2,
friend:{
name:'zlk',
rie:'zhf'
}
}
console.log(obj.x)
console.log(obj.friend?.name)
console.log(obj.friend?.age)
interface Front{
[index:number]:string
}
const front:Front = {
1:'html',
2:'css',
3:'javascript',
4:'vue',
5:'node',
6:'wxcode',
7:'ts',
}
interface Iswim{
swiming:() => void
}
interface Irun{
runing:() => void
}
class person implements Iswim,Irun{
swiming(){
console.log("swiming")
}
runing(){
console.log('runing')
}
}
const p = new person()
function swin(swing:Iswim){
return swing.swiming()
}
swin(p)
enum Direct{
LEFT,
RIGHT,
TOP,
BOTTOM
}
function dir(direct:Direct){
switch (direct) {
case Direct.LEFT:
console.log('left')
break
case Direct.BOTTOM:
console.log('bottom')
break
case Direct.TOP:
console.log('top')
break
case Direct.RIGHT:
console.log('right')
break
default:{
const mydirect:never = direct
}
}
}
dir(Direct.BOTTOM)
dir(Direct.LEFT)
function dir2(num:number):number{
return num * 2
}
function dir3<T,O>(num:T ,num2:O):T{
return num
}
dir3<string,number>('123',1)
dir3<number,string>(123,'123')
interface IFoo<T>{
initialValue:T,
valueList:T[],
handle:(value:T) => T
}
const foo:IFoo<number> = {
initialValue:0,
valueList:[0,1,2],
handle:function (value:number) {
return value
}
}
console.log(foo.valueList)
console.log(foo.handle)
class person2<T>{
x:T
y:T
constructor(x:T,y:T) {
this.x = x
this.y =y
}
}
const p1 = new person2<number>(1,2)
const p2 = new person2(1,2)
const p3:person2<number> = new person2(1,2)
interface Ilength{
length:number
}
function abc<T extends Ilength>(num:T){
console.log(num.length)
}
abc([1,2,3])
abc('abcde')
abc(['a','b','c'])