File: narrowingConstrainedTypeParameter.types

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (43 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
=== tests/cases/compiler/narrowingConstrainedTypeParameter.ts ===
// Repro from #10811

interface Pet {
    name: string;
>name : string
}

function isPet(pet: any): pet is Pet {
>isPet : (pet: any) => pet is Pet
>pet : any

    return typeof pet.name === "string";
>typeof pet.name === "string" : boolean
>typeof pet.name : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>pet.name : any
>pet : any
>name : any
>"string" : "string"
}

export function speak<TPet extends Pet>(pet: TPet, voice: (pet: TPet) => string): string {
>speak : <TPet extends Pet>(pet: TPet, voice: (pet: TPet) => string) => string
>pet : TPet
>voice : (pet: TPet) => string
>pet : TPet

    if (!isPet(pet)) {
>!isPet(pet) : boolean
>isPet(pet) : boolean
>isPet : (pet: any) => pet is Pet
>pet : TPet

        throw new Error("Expected \"pet\" to be a Pet");
>new Error("Expected \"pet\" to be a Pet") : Error
>Error : ErrorConstructor
>"Expected \"pet\" to be a Pet" : "Expected \"pet\" to be a Pet"
    }
    return voice(pet);
>voice(pet) : string
>voice : (pet: TPet) => string
>pet : TPet
}