typescript(TS)由微软推出的
-
typescript>ES6>javascript
-
运行的时候,ts 编译成 javascript
-
静态类型的语言:强类型、遵循ES6、编译器严谨的语法检查
-
vue、react、angular、小程序都推荐用ts, ts默认文件后缀名.ts
Vue3+TS+setup+volar
TS语法格式:
- <script lang="ts" setup> </script>
定义变量:
1、字符串
const str = ref<string>("abc")
const str = ref("abc")
根据值类型自动判断改类型变量是什么
2、数字
const count = ref<number>(10)
const count = ref(10)
3、布尔值
const flag = ref<boolen>(true)
const flag = ref(true)
4、函数
function add(n1:string,n2:number):void{
}
5、接口
定义接口:
interface Iuser = {
name:string,
age:number|string
}
const user = reactive<Iuser>({
name:"shangwu",
age:18
})
在setup中获取props
import{defineProps} from 'vue'
interface Iprops = {min:number,max:number}
const props =defineProps<Iprops>()