Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable undefined check should goes through functions in front of them. #25856

Closed
4 tasks done
RedTailBullet opened this issue Jul 21, 2018 · 4 comments
Closed
4 tasks done
Labels
Duplicate An existing issue was already created

Comments

@RedTailBullet
Copy link

Search Terms

variable, check, undefined

Suggestion

While checking whether a variable is undefined or null, checker should go through all the code including the function runned before the variable is used.

Use Cases

I have a function that required a non-undefined argument. I do the required field check in a separate function and if the required variable is undefined or null, it should throw an error and the variable is not really used.

However, the checker now is still showing me the error that this variable might be undefined.

Examples

class example {
  private property: string

  public assignVariable (variable: string | undefined) {
    this.checkRequiredVariable(variable)
    this.property = variable // This is throwing the "Type 'undefined' is not assignable to type 'string'." error.
  }

  private checkRequiredVariable (variable: string | undefined) {
    if (variable == undefined)
      throw new Error('Variable is undefined or null')
  }
}

PS: This code is just for the example, I know that this will throw a "No initialization" error as well.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@DanielRosenwasser
Copy link
Member

Sounds like you want #17760; is that accurate?

@RedTailBullet
Copy link
Author

@DanielRosenwasser He does mention this kind of thing but mine is not that complicated. If you think it's a duplication I'll accept that. Sorry for the duplicated issue.

@ghost ghost added the Duplicate An existing issue was already created label Jul 22, 2018
@ghost
Copy link

ghost commented Jul 22, 2018

To get type-checking currently, we use a pattern like this:

public assignVariable(variable: string | undefined) {
	this.property = this.checkRequiredVariable(variable)
}

private checkRequiredVariable(variable: string | undefined): string {
	if (variable == undefined)
		throw new Error('Variable is undefined or null')
	return variable
}

You can also make this generic:

function assertDefined<T>(x: T | null | undefined): T {
	if (x == undefined)
		throw new Error("Variable is undefined or null")
	return x
}

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants