JavaScript Can I narrow this down in TypeScript? narrow this down,narrow down this topic,narrow down synonym,

I have a utility function to check whether a variable is not null or undefined, and I want TypeScript to narrow down the input variable if it passes the check, for example:

public init(input?: string): void {
    function isSpecified(input: any): boolean {
        return (typeof input !== "undefined") && (input !== null);
    }

    if (isSpecified(input)) {
        let copiedString: string = input; // <-- Error; input is still 'string | undefined'
    }
}

As you can see TS is not removing the possibility of the string being undefined even though the function makes that logically impossible. Is there a way I can get this function call to narrow down input inside the if block?

Answer:1

You can use a generic type guard function:

public init(input?: string): void {
    function isSpecified<T>(input: null | undefined | T): input is T {
        return (typeof input !== "undefined") && (input !== null);
    }

    if (isSpecified(input)) {
        let copiedString: string = input; // OK
    }
}
Answer:2

Yes, you've basically just written a typeguard function without adding the typeguard.

Change:

function isSpecified(input: any): boolean

to:

function isSpecified(input: any): input is string

More generally, you can use a generic version of the same thing, as mentioned by Ryan:

function isSpecified<T>(input: null | undefined | T): input is T
Answer:3

I need to create a preview of the fields created dynamically in a form. If I click preview button the fields displayed in form should displayed in using AngularJS. Are there any possibilities to do ...

I need to create a preview of the fields created dynamically in a form. If I click preview button the fields displayed in form should displayed in using AngularJS. Are there any possibilities to do ...

I am using Dygraph to display a graph. I have it working statically like this <div id="graphdiv"></div> <script type="text/javascript"> g = new Dygraph( // containing div ...

I am using Dygraph to display a graph. I have it working statically like this <div id="graphdiv"></div> <script type="text/javascript"> g = new Dygraph( // containing div ...

So I update my state in a component and then pass the new props into the child but the child isn't updating correctly and the defaultValue of the input is not changing. At first I thought it might be ...

So I update my state in a component and then pass the new props into the child but the child isn't updating correctly and the defaultValue of the input is not changing. At first I thought it might be ...

  1. react passing props to parent
  2. react passing props to props.children
  3. react passing props from parent to child
  4. react passing props to function
  5. react passing props through route
  6. react passing props down
  7. react passing props to state
  8. react passing props spread operator
  9. react passing props to grandchildren
  10. react passing props to grandchild
  11. react passing props to child components
  12. react passing props with same name
  13. react passing props up
  14. react passing props to this.props.children
  15. react passing props without value

I have been reading about AMD and implementations like requirejs. Most of the resources covers the usage and API's. But, when it comes to implementing this, how can you load a JavaScript file into a ...

I have been reading about AMD and implementations like requirejs. Most of the resources covers the usage and API's. But, when it comes to implementing this, how can you load a JavaScript file into a ...