Typescript type inference not recognizing value must be defined

john_ryan

This might not be possible in typescript but it seems like the TS compiler should be able to figure this out some way. Example here


function validateFoo(val: string | undefined) {
    return val !== undefined ? true : false
}

let myVal: string | undefined

if (validateFoo(myVal)) {
    console.log("output: ", myVal.toLowerCase()) // Error: Object is possibly 'undefined'.
}

it seems like inside the if statement that myVal must not be undefined. However typescript says it may be. Is there some way to annotate that function similar maybe to:

if (!myVal) {
  throw new Error("myVal undefined")
}

console.log(myVal.toLowerCase())

would mark myVal as defined after the throw.

Federkun

That's what type guards does:

function validateFoo(val: string | undefined): val is string {

https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeScript type inference issue

From Dev

TypeScript: Incorrect type inference

From Dev

Typescript union type inference

From Dev

Can function type be defined by inference?

From Dev

TypeScript generics: argument type inference

From Dev

Type inference in TypeScript for curried function

From Dev

Function overloading and type inference in typescript

From Dev

Typescript: Generic class type inference

From Dev

Type inference against type defined in unreferenced namespace

From Dev

Type inference against type defined in unreferenced namespace

From Dev

TypeScript excess field checking and type inference error

From Dev

Typescript: Type inference when using decorator

From Dev

Type inference success depends on naming a value

From Dev

entityName.state must have a defined type

From Dev

Difference in type inference between forEach closure and for loop in Typescript

From Dev

Can VSCode / Typescript do Type inference on plugin-modifed objects?

From Dev

typescript: generic constraints cause type inference to pick wrong candidate?

From Dev

Conditional value declaration of annotated nullable type throws 'type inference failed'

From Dev

Typescript not recognizing Date as Date?

From Dev

Kotlin: unchecked type inference based on function return value

From Dev

Juju not recognizing MAAS defined spaces

From Dev

Recognizing and getting value(s) from pre-defined sentences using PHP (Regex?)

From Dev

Type inference with existential type

From Dev

Recognizing a REAL value

From Dev

In idiomatic Typescript, should I always declare a variable's type, or should I rely more on type inference?

From Dev

"The public type <<classname>> must be defined in its own file" error in Eclipse

From Dev

Disable enforcement of "The public type xyz must be defined in its own file"

From Dev

Build failed--attribute must have a defined type

From Dev

Typescript: Named property type must be assignable to string indexer type

Related Related

  1. 1

    TypeScript type inference issue

  2. 2

    TypeScript: Incorrect type inference

  3. 3

    Typescript union type inference

  4. 4

    Can function type be defined by inference?

  5. 5

    TypeScript generics: argument type inference

  6. 6

    Type inference in TypeScript for curried function

  7. 7

    Function overloading and type inference in typescript

  8. 8

    Typescript: Generic class type inference

  9. 9

    Type inference against type defined in unreferenced namespace

  10. 10

    Type inference against type defined in unreferenced namespace

  11. 11

    TypeScript excess field checking and type inference error

  12. 12

    Typescript: Type inference when using decorator

  13. 13

    Type inference success depends on naming a value

  14. 14

    entityName.state must have a defined type

  15. 15

    Difference in type inference between forEach closure and for loop in Typescript

  16. 16

    Can VSCode / Typescript do Type inference on plugin-modifed objects?

  17. 17

    typescript: generic constraints cause type inference to pick wrong candidate?

  18. 18

    Conditional value declaration of annotated nullable type throws 'type inference failed'

  19. 19

    Typescript not recognizing Date as Date?

  20. 20

    Kotlin: unchecked type inference based on function return value

  21. 21

    Juju not recognizing MAAS defined spaces

  22. 22

    Recognizing and getting value(s) from pre-defined sentences using PHP (Regex?)

  23. 23

    Type inference with existential type

  24. 24

    Recognizing a REAL value

  25. 25

    In idiomatic Typescript, should I always declare a variable's type, or should I rely more on type inference?

  26. 26

    "The public type <<classname>> must be defined in its own file" error in Eclipse

  27. 27

    Disable enforcement of "The public type xyz must be defined in its own file"

  28. 28

    Build failed--attribute must have a defined type

  29. 29

    Typescript: Named property type must be assignable to string indexer type

HotTag

Archive