美文网首页
第十五节: TypeScript模板文字类型

第十五节: TypeScript模板文字类型

作者: 时光如剑 | 来源:发表于2022-05-01 17:43 被阅读0次

    1. 模板文字类型

    模板文字类型是建立在字符串文字类型之上的, 并且能够 通过联合扩展成许多字符串

    当与具体的问题类型一起使用时, 模板文字可以通过连接内容来生成新的字符串文字类型

    例如:

    type World = "world"
    
    // 模板字符串返回新的文字类型
    type Greeting = `hello ${World}`
    // type Greeting = "hello world"
    

    当在插值的位置使用联合类型时, 类型是由每个联合成员表示的每个可能的字符串文字集合

    例如:

    type First = 'Hello' | 'Hi'
    type Name = 'world' | 'siri'
    type AllLocale = `${First | Name}_id`
    // type AllLocale = "Hello_id" | "Hi_id" | "world_id" | "siri_id"
    

    示例中, AllLocaleIds的类型是有插值位置联合类型中的每一个类型, 可能具有的类型和_id组合, 新增所有的可能类型的联合类型

    对于模板文字中 每个插值位置, 联合是交叉相乘的

    type AllLocale = `${First | Name}_id`
    
    type Lang = 'en' | 'zh' | 'cn'
    type LocaleMessage = `${Lang}_${AllLocale}`
    /*
        type LocaleMessage = 
            | "en_Hello_id" 
            | "en_Hi_id" 
            | "en_world_id" 
            | "en_siri_id" 
            | "zh_Hello_id" 
            | "zh_Hi_id" 
            | "zh_world_id" 
            | "zh_siri_id" 
            | "cn_Hello_id" 
            | "cn_Hi_id" 
            | "cn_world_id" 
            | "cn_siri_id"
    */ 
    

    我们通常建议人们对大型字符串联合使用提前生成, 但这较小的情况下很有用

    2. 类型中的字符串联合

    当基于类型内的信息定义一个新的字符串时, 模板文字的力量就来了

    例如:

    // 类型别名
    // on 方法,修改类型参数Type属性名称
    type PropEventSource<Type> = {
      on(eventName:`${string & keyof Type }Changed`, callback:(newValue:any) => void ) : void
    }
    
    // decalre 声明类型, 函数返回交叉类型
    // 其中PropEventSource 类型中有on 方法
    declare function makeWatchedObject<Type>(obj:Type): Type & PropEventSource<Type>
    
    // 调用函数
    const person = makeWatchedObject({
      firstName: 'Saoirse',
      lastName: 'Ronan',
      age: 26
    })
    /*
      person 返回的类型
      const person: 
        {
          firstName: string;
          lastName: string;
          age: number;
        } & PropEventSource<{
            firstName: string;
            lastName: string;
            age: number;
        }>
    */ 
    
    
    // 调用on 方法监听属性变更
    person.on('firstNameChanged',(newValue) =>{
      console.log(`firstName 值变为了${newValue}`)
    })
    

    3. 内置的字符串操作类型

    为了帮助进行字符串操作,TypeScript 包含一组可用于字符串操作的类型。这些类型内置于编译器以提高性能,在TypeScript 包含的文件中找不到。

    3.1 Uppercase<StringType>

    将字符串中的每个字符转换为大写版本。

    例如:

    // 字符串文字类型
    type Greeting = 'hello world'
    // 类型: type Greeting = "hello world"
    
    // 将文字类型转为大写文字类型
    type ShoutyGreeting = Uppercase<Greeting>
    // type ShoutyGreeting = "HELLO WORLD"
    
    // 配合模板文字类型使用
    // 使用类型别名泛型
    type IdGreeting<Str extends string> = `ID_${Uppercase<Str>}` 
    
    type MainId = IdGreeting<'my_app'>
    // type MainId = "ID_MY_APP"
    
    
    3.2 Lowercase<StringType>

    将字符串中的每个字符转换为等效的小写字母。

    例如:

    // 字符串文字类型
    type Greeting = 'Hello World'
    // 类型: type Greeting = 'Hello World'
    
    
    // 将文字类型转为小写文字类型
    type ShoutyGreeting = Lowercase<Greeting>
    // type ShoutyGreeting = "hello world"
    
    // 配合模板文字类型使用
    // 使用类型别名泛型
    type IdGreeting<Str extends string> = `id_${Lowercase<Str>}` 
    
    type MainId = IdGreeting<'my_app'>
    // type MainId = "id_my_app"
    
    
    3.3 Capitalize<StringType>

    字符串中的第一个字符转换为等效的大写字母

    例如:

    // 字符串文字类型
    type Greeting = 'hello world'
    // 类型: type Greeting = "hello world"
    
    
    // 将文字类型转为第一字母大写
    type ShoutyGreeting = Capitalize<Greeting>
    // type ShoutyGreeting = "Hello world"
    
    3.4 Uncapitalize<StringType>

    将字符串中的第一个字符转换为等效的小写字母。

    例如:

    // 字符串文字类型
    type Greeting = 'HELLO WORLD'
    // 类型: type Greeting = "HELLO WORLD"
    
    
    // 将文字类型转为第一字母小写
    type ShoutyGreeting = Uncapitalize<Greeting>
    // type ShoutyGreeting = "hELLO WORLD"
    

    相关文章

      网友评论

          本文标题:第十五节: TypeScript模板文字类型

          本文链接:https://www.haomeiwen.com/subject/ozspyrtx.html