let a : number = 1
let b : string = '1'
let c : boolean = true
let d : string[] = ['1', '2']
let d1 : Array<string> = ['1', '2']
let a1 : any = undefined
let b1 : unknown = null
function getName(): void{
return undefined
}
function getNumber(a:number, b?:string):number{
return 1
}
getNumber(1)
let e : [number, string] = [1, '2']
let f : [number, string?] = [1]
let g : [number, ...string[]] = [1, '2', '3']
const enum a11 { up, down, left = 5, right }
const enum a22 {up = 'up', down = 'down'}
let h : '男' = '男'
let i : {name: string, age: number} = {name: '张三', age: 18 }
let j : {[key:string]: string} = {name: '李四', sex: '男'}
type shuzi = number
let k : shuzi = 1
type shuzizifuchuan = number | string
type sex = '男' | '女'
let l : shuzizifuchuan = '1'
type shuzizifuchuan1 = {name: string} & {age:number}
let m : shuzizifuchuan1 = {name: '成', age: 22 }
let n : sex = '男'
class Pwrson {
public name : string
protected age : number
private readonly sex : String
constructor(name: string, age:number, sex: string ){
this.name = name
this.age = age
this.sex = sex
}
public speak():string{
return `${this.name}今年${this.age},是一个${this.sex}神`
}
}
const p1 = new Pwrson('程创', 18, '男')
console.log(p1.speak())
class Person {
constructor(public name: string, protected age: number, private sex : string ){}
}
abstract class Package{
constructor(public weight: number){}
abstract calculate():number
printPackage(){
console.log(`包裹重量为${this.weight}kg,运费为:${this.calculate()}元`)
}
}
class StandardPackage extends Package {
constructor(weight: number,public unitPrice : number){
super(weight)
}
calculate(): number {
return this.weight * this.unitPrice
}
}
const s1 = new StandardPackage(10, 20)
s1.printPackage()
interface Iperson {
name: string
age: number
speak(n:number): void
}
class Person1 implements Iperson{
constructor(public name: string, public age : number){}
speak(n: number): void {
console.log(`你好${n}。我叫${this.name},我今年${this.age}`);
}
}
const person2 = new Person1('程创', 31)
person2.speak(1)
interface Iuser {
name : String
readonly gender: string
age?: number
run:(n:number) => void
}
const user: Iuser = {
name : "张三",
gender : '男',
run(n) {
console.log(`${this.name}奔跑了${n}米`)
}
}
user.run(200);
user.name = '李四'
user.run(200)
interface Icount{
(a:number, b:number):number
}
const count: Icount = (x, y) => {
return x + y
}
interface PersonInterFace {
name: string
age: number
}
interface studentInterface extends PersonInterFace {
grade : string
}
const stu:studentInterface = {
name: '张三',
age: 18,
grade: '初二',
}
interface per3 {
name : string,
age: number,
}
interface per3 {
sex: string
}
const person3 : per3 = {
name: '张三',
age: 18,
sex: '男'
}
function logData<T>(data:T){
console.log(data)
}
function log4g<T, U>(data1:T, data2:U){
console.log(data1, data2)
}
logData<number>(100)
logData('dsafd')
log4g<string, number>('hello', 666)
type JobInfo = {
title: string;
company: string;
}
interface PersonInterF1ace<T>{
name: string,
age: number,
extraInfo: T
}
let p : PersonInterF1ace<JobInfo> = {
name: 'tom',
age: 18,
extraInfo: {
title: '报社',
company: '齐鲁公司'
}
}
class Person11<T>{
constructor(
public name: string,
public age: number,
public extraInfo: T
){}
speak(){
console.log(`我叫${this.name},今年${this.age}岁了。`)
console.log(this.extraInfo)
}
}
const p22 = new Person11<number>('tom', 30, 250)
const Demo = function (target : Function){
console.log(target, 333)
target.prototype.test1 = ()=>{
console.log('hello, 12313')
}
target.prototype.test1();
target.prototype.sex = '男'
console.log(target.prototype, 444)
}
@Demo
class test {
name: string = '男'
constructor(public age: number){}
test1(){}
}
const test1 = new test(14)
console.log((test1 as any).sex, 66666);
test1.test1()
function test22(){
const a = 11
function b () {
console.log('你好')
}
console.log('hello')
}
type Constructor = new (...args:any[]) => {}
function test33(fn:Constructor) {}
@test33
class person33{}
function LogTime<T extends Constructor>(target: T){
return class extends target{
createdTime: Date
constructor(...args: any[]){
super(...args)
this.createdTime = new Date()
}
getTime(){
return `该对象的创建时间是${this.createdTime}`
}
}
}
@LogTime
class Person4 {
constructor(public name:string, public sex: string){}
speack():void{
console.log(`${this.name}说,我是${this.sex}的`)
}
}
interface Person4{
createdTime: Date,
getTime():void
}
const person44 = new Person4('程创', '男')
person44.speack()
console.log(person44.createdTime)
console.log(person44.getTime())
function LogInfo(n:number){
return function(target: Function){
target.prototype.introduct = function () {
for(let i = 0; i<n; i++){
console.log('你好。我有一个帽衫');
}
}
}
}
interface Person88{
introduct(): void
}
@LogInfo(5)
class Person88 {
constructor(public name:string, public age:number){}
speak(){
console.log('你好啊!')
}
}
const person88 = new Person88('程创', 16)
person88.introduct()
function Demo1 (target:object, propertyKey:string){
console.log(target, propertyKey);
}
function State(target: object, propertyKey: string){
let key = `__${propertyKey}`
Object.defineProperty(target, propertyKey, {
get(){
return this.key
},
set(newVal){
console.log(`${propertyKey}的最新值为:${newVal}`)
this[key] = newVal
}
})
}
class Person9{
@State public name : string
@Demo1 static school : string
constructor( name:string, public age: number){
this.name = name
}
}
const person99 = new Person9('程创', 18)
person99.name = '22'
function FunSpeak(target: object, propertyKey: string, descriptor:PropertyDescriptor){
console.log(descriptor, 333333);
const originnal = descriptor.value
descriptor.value = function(...args:any[]){
console.log(`${propertyKey}开始执行....`);
const result = originnal.call(this, ...args)
console.log(`${propertyKey}执行完毕....`);
return result
}
}
class Person10{
public name : string
static school : string
constructor( name:string, public age: number){
this.name = name
}
@FunSpeak
speak(){
console.log(`你好。我得名字是${this.name}, 我得年龄${this.age}`)
}
}
const person101 = new Person10('张三', 15)
person101.speak()