TypeScript Specify Type For Individual Function Use

Jon Lamb

I have a function that returns multiple types (union type) but then I have another function later that uses the value returned from the first function but only accepts a single type. Right now typescript is giving me an error saying that there is a type mismatch because the first function could potentially return something that would not be acceptable for the second function, but in the way that I am calling it I know it will not have a conflict. How can I inform typescript that the return value of the first method will work in the second?

Example:

function returnNumberOrString (variable): string | number {
    if (variable === 'hello') return 'hello to you';
    if (variable === 123) return 456;
}

function onlyNumbers (number : number) {
    return number + 1;
}

let number = returnNumberOrString(123);

onlyNumbers(number); // This shows a warning because returnNumberOrString could possibly return a string
James

The accepted solution only hides the compiler error. You will still be able to call onlyNumbers with a string even if you use Type Assertion to tell the compiler it is a number. If you perform the check I've written below, you won't ever pass anything but a number to your onlyNumbers function. This is a safer operation if you're doing anything that requires strictly numbers (as your function name implies)

function returnNumberOrString (variable): string | number {
    if (variable === 'hello') return 'hello to you';
    if (variable === 123) return 456;
}

function onlyNumbers (number : number) {
    return number + 1;
}

// Can pass string or number, will only ever get a number in val
let val = typeof returnNumberOrString(123) === "number" ? 456 : -1; // I used -1, you can use any value to mean "error" though.

console.log(val); // 456
console.log(onlyNumbers(val)); // 457

let val2 = typeof returnNumberOrString("hello") === "number" ? 456 : -1; 

console.log(val2); // -1
console.log(onlyNumbers(val2)); // 0 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I specify the function type in my type hints?

From Java

Specify return type in TypeScript arrow function

From Java

Can You Specify Multiple Type Constraints For TypeScript Generics

From Dev

How to specify that a function returns a certain type when new'ed in TypeScript?

From Dev

How can I specify a return type for a $http call with Typescript?

From Dev

Use jQuery .click() function with an individual element

From Dev

Declaring the type of 'this' in a typescript function?

From Dev

return type for class function typescript

From Dev

Is there a standard way to specify the type of a callback function in a docstring?

From Dev

Is it possible to use a variable and not specify a return type in postgreSQL?

From Dev

Type inference in TypeScript for curried function

From Dev

How to specify a generic type for a function parameter

From Dev

Type for parameter in TypeScript Arrow Function

From Dev

Function type annotation typescript

From Dev

How to extract the type of a function in typescript?

From Dev

Generic return type of function in Typescript

From Dev

Typescript: Specify type in object declaration

From Dev

How to dynamically specify type in TypeScript generics?

From Dev

Setting the type of an anonymous function TypeScript

From Dev

Function overloading and type inference in typescript

From Dev

How to specify type of index of enum in typescript

From Dev

TypeScript - Extend type returned by a function

From Dev

Type guards for function in TypeScript

From Dev

Is it possible to use a constructor function as a type of an argument of another function in TypeScript?

From Dev

specify a type in the function specification in Elixir

From Dev

Typescript strongly type void Function

From Dev

Specify variable type passing function as a prop

From Dev

Typescript function type assignment issue

From Dev

How to specify Typescript Mongoose model type in await

Related Related

  1. 1

    How can I specify the function type in my type hints?

  2. 2

    Specify return type in TypeScript arrow function

  3. 3

    Can You Specify Multiple Type Constraints For TypeScript Generics

  4. 4

    How to specify that a function returns a certain type when new'ed in TypeScript?

  5. 5

    How can I specify a return type for a $http call with Typescript?

  6. 6

    Use jQuery .click() function with an individual element

  7. 7

    Declaring the type of 'this' in a typescript function?

  8. 8

    return type for class function typescript

  9. 9

    Is there a standard way to specify the type of a callback function in a docstring?

  10. 10

    Is it possible to use a variable and not specify a return type in postgreSQL?

  11. 11

    Type inference in TypeScript for curried function

  12. 12

    How to specify a generic type for a function parameter

  13. 13

    Type for parameter in TypeScript Arrow Function

  14. 14

    Function type annotation typescript

  15. 15

    How to extract the type of a function in typescript?

  16. 16

    Generic return type of function in Typescript

  17. 17

    Typescript: Specify type in object declaration

  18. 18

    How to dynamically specify type in TypeScript generics?

  19. 19

    Setting the type of an anonymous function TypeScript

  20. 20

    Function overloading and type inference in typescript

  21. 21

    How to specify type of index of enum in typescript

  22. 22

    TypeScript - Extend type returned by a function

  23. 23

    Type guards for function in TypeScript

  24. 24

    Is it possible to use a constructor function as a type of an argument of another function in TypeScript?

  25. 25

    specify a type in the function specification in Elixir

  26. 26

    Typescript strongly type void Function

  27. 27

    Specify variable type passing function as a prop

  28. 28

    Typescript function type assignment issue

  29. 29

    How to specify Typescript Mongoose model type in await

HotTag

Archive