当前位置:网站首页>[TS] type alias

[TS] type alias

2022-06-29 01:20:00 A meteor unwilling to fall

List of articles

Basic usage

  • type: Type aliases are used to give a type a new name , You can use the same type multiple times and use the same name .

  • type Type alias syntax :

type Point = {
    
  x: number;
  y: number;
};
 
function printCoord(pt: Point) {
    
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}
 
printCoord({
     x: 100, y: 100 });
  • You can use a type alias to name any type , Not just object types .
  • Commonly used for union types .
type test = number; // Basic types 
let num: test = 10;
type userOjb = {
    name:string} //  object 
type getName = ()=>string  //  function 
type data = [number,string] //  Tuples 
type numOrFun = number | getName  //  Joint type 
原网站

版权声明
本文为[A meteor unwilling to fall]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202161215310073.html