Install the TypeScript environment
npm install typescript -g
npm install ts-node -g
Copy the code
Create an index.ts file
console.log(Hello TypeScript)
ts-node index.ts
Copy the code
Not surprisingly, it prints “Hello TypeScript.”
If something goes wrong I can only say that I use Windows CMD to use it
TypeScript variable types
TypeScript One of the biggest features is that variables are strongly typed, which means that when declaring a variable, we must give it a type.
Data types in TypeScript include:
- Undefined :
- Number: indicates the numeric type.
- String: a string.
- Boolean: Boolean type;
- Enum: enumeration type.
- Any: any type.
- Void: empty type;
- Array: Array type;
- Tuple: indicates the ancestor type.
- Null: empty type;
For 🌰 :
let age:number = 18
console.log(I didn't `${age}At the age of `)
Copy the code
Smart you will be able to draw inferences from one another!
The TypeScript function
Define a function:
function notUpdate(age:number) :string{
return 'Stop updating, I can't learn, I just${age}At the age of `
}
Copy the code
When I define the function, I set the parameter type in advance, and of course I set the return value
Functions with or without passable parameters:
function notUpdate(name:string,excitation? :string) :string{
if(excitation ! =undefined) {return 'I'm a pirate${name}Is to become${excitation}The `
}
return 'I'm a pirate, Wang${name}`
}
Copy the code
Class:
A class is an abstraction of an object-concrete transaction, and an object is a concrete representation of a class.
An 🌰 :
When someone sets you up with someone, they ask what you want. So, your requirements are: height 165 +, lean build, long hair, big eyes. Having a proper, stable job, cooking, etc. These requirements are an abstraction of your ideal mate, the class. The girls that the introducer gives you according to your requirements are instances of the class and objects.
Look at the following code:
class Zhaoduixiang{
name:string;
age:number;
constructor(name:string,age:number){
this.name=name;
this.age=age;
}
say(){
console.log('Hello, my name isThe ${this.name}This year,The ${this.age}Age, I am the number one female guest! `)}}let duixiang:Zhaoduixiang = new Zhaoduixiang('little Lin'.18)
console.log(duixiang)
duixiang.say()
Copy the code
Class modifiers:
- Access modifiers are public, protected, and private
- Public: A public modifier that can be used to modify properties or behaviors within or outside a class. The default modifier.
- Protected: Protected modifiers. You can use protected attributes and behaviors in this class and subclasses.
- Private: A private modifier that can only be used within a class to modify properties and behaviors that are private.
- Go to 🌰 with just blind date:
class Zhaoduixiang{
public sex:string
protected name:string
private age:number
public constructor(sex:string,name:string,age:number){
this.name=name
this.age=age
}
public sayFirst(){
console.log('Hello, my name isThe ${this.name}This year,The ${this.age}Age, I am the number one female guest! `)
}
protected sayNext(){
console.log('I've got a crush on you! I'm gonna give you a baby! ')}}let duixiang:Zhaoduixiang = new Zhaoduixiang('little Lin'.18) console.log(duixiang.sex)
console.log(duixiang.name) / / an error
console.log(duixiang.age) / / an error
duixiang.sayHello()
duixiang.sayLove() / / an error
Copy the code
Class inheritance and rewriting:
Inheritance: allows us to create a class (subclass) that inherits all properties and methods from an existing class (parent class). Subclasses can create new properties and methods that do not exist in the parent class.
Override: A method that overrides a parent class in a subclass.
class Lizi{
public name:string
public age:number
constructor(name:string,age:number){
this.name=name
this.age=age
}
public say(){
console.log(I call `The ${this.name}This year,The ${this.age}`)}}/ / inheritance extends
class Skill extends Lizi{
public skills:string
constructor(name:string,age:number,skills:string){
super(name,age)
this.skills=skills
}
public saySkill(){
console.log(I call `The ${this.name}This year,The ${this.age}, it is aThe ${this.skills}`)}// Method override
public say(){
console.log('I overwrote the method.')}}let skill:Skill=new Skill('luffy'.18.'pirates')
skill.saySkill()
Copy the code
The TypeScript interface
Interface: Define a type for your named variable, keyword: interface
🌰 :
interface LiziFace{
name:string age:number sex? :string }let LiziVal:LiziFace={name:'her beauty'.age:18.sex:'woman'}
console.log(LiziVal)
class Lizi implements LiziFace{
public name:string
public age:number
constructor(name:string,age:number){
this.name=name
this.age=age
}
public say(){
console.log(I call `The ${this.name}This year,The ${this.age}`)}}let lizi:Lizi=new Lizi('luffy'.18)
lizi.say()
Copy the code
TypeScript namespaces
As more validators were added, we needed a way to organize our code so that we could record their types without worrying about naming conflicts with other objects.
🌰 :
namespace name1{
export class Lizi{
public name:string='name1çš„name'
talk(){
console.log(I was `The ${this.name}`)
}
}
}
namespace name2{
export class Lizi{
public name:string='name2çš„name'
talk(){
console.log(I was `The ${this.name}`)}}}let lizi1:name1.Lizi = new name1.Lizi()
let lizi2:name2.Lizi = new name2.Lizi()
lizi1.talk()
lizi2.talk()
Copy the code
While learning typescript, I also reviewed the basics of typescript. It’s a very happy day. If you find any mistakes, please let me know and lead me to progress together.