必填参
    参数一旦声明,就要求传递,且类型需符合,

    1. function greeting(person: string): string {
    2. return "Hello, " + person;
    3. }
    4. greeting('tom')

    可选参数
    参数名后面加上问号,变成可选参数,

    1. function greeting(person: string, msg?: string): string {
    2. return "Hello, " + person;
    3. }
    4. greeting('tom')
    5. greeting('tom', 'glad to see you')

    默认值
    跟可选参数相似,但是不传参时,会使用默认的参数,

    1. function greeting(person: string, msg = ""): string {
    2. return "Hello, " + person;
    3. }
    4. greeting("tom");
    5. greeting("tom", "glad to see you");

    函数重载
    以参数数量或类型区分多个同名函数,

    1. // 重载1
    2. function watch(cb1: () => void): void;
    3. // 重载2
    4. function watch(cb1: () => void, cb2: (v1: any, v2: any) => void): void;
    5. // 实现
    6. function watch(cb1: () => void, cb2?: (v1: any, v2: any) => void) {
    7. if (cb1 && cb2) {
    8. console.log("执行watch重载2");
    9. } else {
    10. console.log("执行watch重载1");
    11. }
    12. }
    13. // 执行watch重载1
    14. watch((): void => {
    15. console.log(123);
    16. });
    17. // 执行watch重载2
    18. watch(
    19. (): void => {
    20. console.log(123);
    21. },
    22. (): void => {
    23. console.log(321);
    24. }
    25. );
    1. <template>
    2. <div>
    3. <input type="text" placeholder="输入新特性" @keyup.enter="addFeature" />
    4. </div>
    5. </template>
    6. <script lang="ts">
    7. import { Component, Vue } from "vue-property-decorator";
    8. @Component
    9. export default class HelloWorld extends Vue {
    10. addFeature(e: KeyboardEvent) {
    11. const target = e.target as HTMLInputElement;
    12. console.log(target.value);
    13. }
    14. }
    15. </script>