What is the purpose of this code in below?

mustafa kemal tuna
const fullNameMaxLength = 10;

class Employee {
private _fullName: string;

set fullName(newName: string) {
    if (newName && newName.length > fullNameMaxLength) {
        throw new Error("fullName has a max length of " + fullNameMaxLength);
    }

    this._fullName = newName;
}
}

if (newName && newName.length > fullNameMaxLength)

I can understand to check that newName is truthy or not in vanillaJS but, in Typescript what is the purpose of that? Typescript already guarantees that newName is string and it has .length property.

full code is here: https://www.typescriptlang.org/docs/handbook/classes.html

Alex Wayne

Assuming all code is typescript, and compiled by typescript, and no value involved is any, then this existence check serves no purpose.

However, as other commenters point out, if any code is plain javascript, or exposed as a library to be used on unknown codebases or environments, then type safety cannot be guaranteed. So it's up to the developer to decide to do double checks.

But, here's some ways that it might still be good to write it that way:

  1. non-typescript caller
// foo.js
employee.fullName = null // not typescript so no error.
  1. a type of any might be passed in
// foo.ts
const jsonData: any = { employees: [{ fullName: null }] } // data from a remote api

// no error because `any` be cast to any other type without error.
employee.fullName = jsonData.employees[0].fullName is any.

The existence check would guard against an error being thrown by both of these cases. However, the very next line would assign the bad value anyway...

I would argue that, as an example in the typescript docs, the existence check probably should not be there, as it is a bit confusing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

what would be the purpose of using the toString() method in the code below?

In Swift what is purpose of using UIBackgroundTaskIdentifier. how below mentioned code will execute

what is the purpose of using template.library() and @register.filter() in the below code

What is the purpose of this code

What is the purpose this code "SomeString" & ""=""

What is the purpose of this JavaScript code?

Purpose of using set method in below code snippet

What is the use of "*&" in the code below

What are the differences with the code below?

what is the mean of \+ in the below code

what is the issue with the below code

what is the purpose of return_string = " " in the code below. Additionally what is happening in line 4 when " " is added to str(x)

What is the purpose of .enumerated() and .map() in this code?

what is the purpose of Specific line in the Code

What is the purpose of authorization code in OAuth

What is the purpose of "Sections" in Octave code?

what is the the purpose of using ? and : in the following code

What is the purpose of 'turn' in this line of code?

What is the purpose of the nested promise in this code?

What is the purpose of each of these code lines?

What will be the time complexity of the below code?

What is time complexity of below code?

What will be EOF here in the below code?

What will be the output of the given code below?

What is the output of the below python code

what is the uni code encoding error in the code below

What is the purpose of this code? Is it just duplicating a Date?

What is the purpose of the function (toEnum . fromEnum) in Haskell code?

What is the purpose of the Linux home partition code 8302?