TypeScript 基础类型

TypeScript 复制代码
//基础类型 string,number,boolean
const teacherName:string = 'chen lee';
const teacherAge:number = 28.0;
const isMale:boolean = true;
//数组
const numberArr:number[]=[1,2,3,4,5];
const stringArr:string[]=['1','2','3','4','5'];
const booleanArr:Array<boolean>=[true,false,true];
//对象类型
const user: {name:string,age:number} = {name:'dell',age:18};
const userOne: {name:string,age?:number} = {name:'dell'};

function union(id:string|number)
{
    if(typeof id ==='string')
    {
        console.log(id.toUpperCase());
    }
    else
    {
        console.log(id);
    }
}
union('asd');
type User = {name: string,age:number};
//type Employee = {name: string,age:number,salary:number};
const userTwo:User = {name:'dell',age:18};
const userThree:User = {name:'dell',age:18};
//any
function showMessage(message:any)
{
    console.log(message);
}
//函数类型
function abc(message:string):number
{
    return 1;
}
const def: (age:number)=>number = (age:number)=>{return 1;}
//接口类型Interface
interface Student
{
    age:number;
    sex?:string;
}
//继承
// interface OldStudent extends Student
// {
//     name: string;
// }
//const student:Student = {age:18,sex:'male'};
//const oldstudent:OldStudent = {age:18,sex:'male',name:'dell'};
interface Student
{
    name: string;
}
const student:Student = {age:18,sex:'male',name:'dell'};

//交叉类型
type Employee = User & {salary:number};
const employee:Employee = {name: 'string',age:13,salary:18};
//断言 Assersion
//const dom: HTMLElement = document.getElementById('#root');
//const dom1: undefined = document.getElementById('#root') as undefined;
//const dom2: undefined = <undefined>document.getElementById('#root');
const testString: string = 123 as any as string;

//字面量类型
const str:'ast'= 'ast';
function getPosition(position:'left'|'right'):string
{
    return position;
}

const truthy: true = true;
//
function request(url: string,method:'GET'|'POST'):string
{
    return 'sending request';
}
const params:{url:string,method:string}={url:'imooc',method:'POST'}
request(params.url,params.method as 'GET');
//null , undefined
//严格校验会报红
//const testObject:null = undefined;
//const testUndefined:undefined = null;
const testObject:undefined = undefined;
const testUndefined:null = null;
function checkNull(abc:string | null)
{
    if(typeof abc === 'string' )
    {
        console.log(abc.toUpperCase());
    }
    //console.log(abc!.toUpperCase());表示一定非null
}
//void类型
function getNumber():void//无返回值
{

}

tsconfig.json

bash 复制代码
{
    "compilerOptions":{
        "noImplicitAny":true,
        "strictNullChecks": true
    }
}

"noImplicitAny":true,//对类型转换做检查

"noImplicitAny": true 配置在 TypeScript 项目中,意味着‌禁止隐式使用 any 类型 ‌。当 TypeScript 编译器无法推断出某个变量、参数或属性的类型时,会‌报编译错误 ‌,而不是默认将其视为 any

核心作用

  • 强制显式类型声明‌:所有未标注类型的标识符必须明确指定类型,否则编译失败。
  • 提升代码安全性‌:避免因类型推断失败导致的潜在运行时错误。
  • 增强可维护性‌:代码意图更清晰,便于团队协作和长期维护。

"strictNullChecks": true//对null和undefined做检查

核心作用

  • 禁止隐式空值赋值 ‌:在严格模式下,nullundefined不能 ‌赋值给其他类型(如 stringnumber 等),除非显式声明为联合类型(如 string | null)‌‌。
  • 强制空值检查 ‌:访问变量属性前必须先判断其是否为 nullundefined,避免运行时错误‌‌。
  • 类型推断更精确 ‌:变量赋值为 null 会被推断为 null 类型,赋值为 undefined 会被推断为 undefined 类型,而非 any‌‌。
相关推荐
言乐614 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
WWJA王文举21 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
zhanghaha131421 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
qq_448011161 天前
C语言中的动态内存分配
c语言·开发语言·php
汉字萌萌哒1 天前
2024CSP-J入门级C++真题详解
开发语言·c++
arbboter1 天前
【网络工具】NetProxy网络代理用户手册
开发语言·网络·c#·网络代理·网络异常·代理工具
汉字萌萌哒1 天前
2019CCF-CSP入门级C++试题解析
java·开发语言·c++
叠层归一研究院1 天前
AGI 系统(十一):二阶网络 — 二阶递归 × 多主体 (元符号网络)
开发语言·人工智能·算法·php·agi
小雨笙笙1 天前
C语言:变量三属性——存储类型
c语言·开发语言
想要成为糕糕手1 天前
🚀 在浏览器里跑 DeepSeek-R1?WebGPU 端侧推理实战(六)—— 完结篇:消息渲染与流式收尾
react.js·typescript·llm