TypeScript 3 form validation object











up vote
1
down vote

favorite
1












Please, correct me with anything that I say in here (The actual question is below the code).



I've been quickly prototyping a project (ASP.NET Core 2.1) and haven't found the need to structure it with Webpack/Parcel/Rollup/Browserify as it's done with simple JavaScript validations per views (I do minify the files with Gulp).



Since I'm not enclosing my files into modules, they're all open to the global scope (bad idea, right?). Therefore, I've decided to "namespace" them by running everything inside a JavaScript variable.



The code that I have looks like this:



const formValidation = {
submitBtn : null as unknown as HTMLButtonElement,
errors: null as unknown as string,
errorsDiv: null as unknown as HTMLDivElement,
gradeSections: null as unknown as HTMLDivElement,
form: null as unknown as HTMLFormElement,
mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
this.errorsDiv = document.getElementById('js-errors') as HTMLDivElement;
this.gradeSections = document.getElementById('js-grade-sections') as HTMLDivElement;
this.form = document.getElementById('js-submit-form') as HTMLFormElement;
this.mapEventListenersFormValidation();
},
mapEventListenersFormValidation() {
this.submitBtn.addEventListener('click', this.submit.bind(this));
},
submit(evt: Event) {
console.log(evt);
evt.preventDefault();
this.resetForms();
if (!this.checkIfSubmitIsValid()) {
this.showErrors();
return;
}
this.form.submit();
},
checkIfSubmitIsValid() {
this.errors = ;
const subjectSelected = this.isSubjectSelected();
const sectionsSelected = this.areSectionsSelected();

if (!subjectSelected) {
selectSubjectDropdown.classList.add('is-invalid');
this.errors.push('Debe de seleccionar por lo menos una materia');
}

if (!sectionsSelected) {
this.gradeSections.classList.add('form-error');
this.errors.push('Debe de seleccionar al menos una sección');
}

return subjectSelected && sectionsSelected;
},
isSubjectSelected() {
return selectSubjectDropdown.selectedIndex !== 0;
},
areSectionsSelected() {
return selectedSectionCounter > 0;
},
showErrors() {

if (!this.errorsDiv || !this.errorsDiv.parentElement) {
return;
}
this.errorsDiv.parentElement.removeAttribute('hidden');
this.errorsDiv.innerHTML = '';
this.errors.forEach((error) => {
const li = document.createElement('li');
li.innerHTML = error;
this.errorsDiv.appendChild(li);
});
},
resetForms() {
selectSubjectDropdown.classList.remove('is-invalid');
this.gradeSections.classList.remove('form-error');
},

};

DOMReady(formValidation.mapDOM.bind(formValidation));


Check the submitBtn, errors, and errorsDiv properties. AFAIK, JavaScript doesn't have a way to declare an "empty" property. Therefore something sane would be to assign it either undefined or null.



As I want to have it statically defined (through TypeScript), I want to assign a type to those properties. The problem is that I just can't assign a non-nullable type to a null.



As I'm trying to avoid non-implicit any's in the scripts, the compiler suggested me to use unknown.



The question is, whether it's ok for me to use the unknown type from TypeScript to cast a null to an unknown so it can let me cast it to the type that I'm going after.



Edit Ended up adding the whole document into it. I was working with that function, so it's different now.










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • The code to be reviewed looks sketchy or incomplete: where are checkIfSubmitIsValid() and showErrors() defined?
    – 200_success
    Nov 1 at 14:14










  • @200_success: Oh they're not needed for what I'm looking for. It's the second and third line of the code snippet above.
    – Jose A
    Nov 1 at 23:44










  • Maybe they're not needed for what you are looking for, but context is still important for reviewers.
    – 200_success
    Nov 1 at 23:55










  • @200_success: Got it! I'll update it asap! Sorry
    – Jose A
    Nov 2 at 0:07















up vote
1
down vote

favorite
1












Please, correct me with anything that I say in here (The actual question is below the code).



I've been quickly prototyping a project (ASP.NET Core 2.1) and haven't found the need to structure it with Webpack/Parcel/Rollup/Browserify as it's done with simple JavaScript validations per views (I do minify the files with Gulp).



Since I'm not enclosing my files into modules, they're all open to the global scope (bad idea, right?). Therefore, I've decided to "namespace" them by running everything inside a JavaScript variable.



The code that I have looks like this:



const formValidation = {
submitBtn : null as unknown as HTMLButtonElement,
errors: null as unknown as string,
errorsDiv: null as unknown as HTMLDivElement,
gradeSections: null as unknown as HTMLDivElement,
form: null as unknown as HTMLFormElement,
mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
this.errorsDiv = document.getElementById('js-errors') as HTMLDivElement;
this.gradeSections = document.getElementById('js-grade-sections') as HTMLDivElement;
this.form = document.getElementById('js-submit-form') as HTMLFormElement;
this.mapEventListenersFormValidation();
},
mapEventListenersFormValidation() {
this.submitBtn.addEventListener('click', this.submit.bind(this));
},
submit(evt: Event) {
console.log(evt);
evt.preventDefault();
this.resetForms();
if (!this.checkIfSubmitIsValid()) {
this.showErrors();
return;
}
this.form.submit();
},
checkIfSubmitIsValid() {
this.errors = ;
const subjectSelected = this.isSubjectSelected();
const sectionsSelected = this.areSectionsSelected();

if (!subjectSelected) {
selectSubjectDropdown.classList.add('is-invalid');
this.errors.push('Debe de seleccionar por lo menos una materia');
}

if (!sectionsSelected) {
this.gradeSections.classList.add('form-error');
this.errors.push('Debe de seleccionar al menos una sección');
}

return subjectSelected && sectionsSelected;
},
isSubjectSelected() {
return selectSubjectDropdown.selectedIndex !== 0;
},
areSectionsSelected() {
return selectedSectionCounter > 0;
},
showErrors() {

if (!this.errorsDiv || !this.errorsDiv.parentElement) {
return;
}
this.errorsDiv.parentElement.removeAttribute('hidden');
this.errorsDiv.innerHTML = '';
this.errors.forEach((error) => {
const li = document.createElement('li');
li.innerHTML = error;
this.errorsDiv.appendChild(li);
});
},
resetForms() {
selectSubjectDropdown.classList.remove('is-invalid');
this.gradeSections.classList.remove('form-error');
},

};

DOMReady(formValidation.mapDOM.bind(formValidation));


Check the submitBtn, errors, and errorsDiv properties. AFAIK, JavaScript doesn't have a way to declare an "empty" property. Therefore something sane would be to assign it either undefined or null.



As I want to have it statically defined (through TypeScript), I want to assign a type to those properties. The problem is that I just can't assign a non-nullable type to a null.



As I'm trying to avoid non-implicit any's in the scripts, the compiler suggested me to use unknown.



The question is, whether it's ok for me to use the unknown type from TypeScript to cast a null to an unknown so it can let me cast it to the type that I'm going after.



Edit Ended up adding the whole document into it. I was working with that function, so it's different now.










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • The code to be reviewed looks sketchy or incomplete: where are checkIfSubmitIsValid() and showErrors() defined?
    – 200_success
    Nov 1 at 14:14










  • @200_success: Oh they're not needed for what I'm looking for. It's the second and third line of the code snippet above.
    – Jose A
    Nov 1 at 23:44










  • Maybe they're not needed for what you are looking for, but context is still important for reviewers.
    – 200_success
    Nov 1 at 23:55










  • @200_success: Got it! I'll update it asap! Sorry
    – Jose A
    Nov 2 at 0:07













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





Please, correct me with anything that I say in here (The actual question is below the code).



I've been quickly prototyping a project (ASP.NET Core 2.1) and haven't found the need to structure it with Webpack/Parcel/Rollup/Browserify as it's done with simple JavaScript validations per views (I do minify the files with Gulp).



Since I'm not enclosing my files into modules, they're all open to the global scope (bad idea, right?). Therefore, I've decided to "namespace" them by running everything inside a JavaScript variable.



The code that I have looks like this:



const formValidation = {
submitBtn : null as unknown as HTMLButtonElement,
errors: null as unknown as string,
errorsDiv: null as unknown as HTMLDivElement,
gradeSections: null as unknown as HTMLDivElement,
form: null as unknown as HTMLFormElement,
mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
this.errorsDiv = document.getElementById('js-errors') as HTMLDivElement;
this.gradeSections = document.getElementById('js-grade-sections') as HTMLDivElement;
this.form = document.getElementById('js-submit-form') as HTMLFormElement;
this.mapEventListenersFormValidation();
},
mapEventListenersFormValidation() {
this.submitBtn.addEventListener('click', this.submit.bind(this));
},
submit(evt: Event) {
console.log(evt);
evt.preventDefault();
this.resetForms();
if (!this.checkIfSubmitIsValid()) {
this.showErrors();
return;
}
this.form.submit();
},
checkIfSubmitIsValid() {
this.errors = ;
const subjectSelected = this.isSubjectSelected();
const sectionsSelected = this.areSectionsSelected();

if (!subjectSelected) {
selectSubjectDropdown.classList.add('is-invalid');
this.errors.push('Debe de seleccionar por lo menos una materia');
}

if (!sectionsSelected) {
this.gradeSections.classList.add('form-error');
this.errors.push('Debe de seleccionar al menos una sección');
}

return subjectSelected && sectionsSelected;
},
isSubjectSelected() {
return selectSubjectDropdown.selectedIndex !== 0;
},
areSectionsSelected() {
return selectedSectionCounter > 0;
},
showErrors() {

if (!this.errorsDiv || !this.errorsDiv.parentElement) {
return;
}
this.errorsDiv.parentElement.removeAttribute('hidden');
this.errorsDiv.innerHTML = '';
this.errors.forEach((error) => {
const li = document.createElement('li');
li.innerHTML = error;
this.errorsDiv.appendChild(li);
});
},
resetForms() {
selectSubjectDropdown.classList.remove('is-invalid');
this.gradeSections.classList.remove('form-error');
},

};

DOMReady(formValidation.mapDOM.bind(formValidation));


Check the submitBtn, errors, and errorsDiv properties. AFAIK, JavaScript doesn't have a way to declare an "empty" property. Therefore something sane would be to assign it either undefined or null.



As I want to have it statically defined (through TypeScript), I want to assign a type to those properties. The problem is that I just can't assign a non-nullable type to a null.



As I'm trying to avoid non-implicit any's in the scripts, the compiler suggested me to use unknown.



The question is, whether it's ok for me to use the unknown type from TypeScript to cast a null to an unknown so it can let me cast it to the type that I'm going after.



Edit Ended up adding the whole document into it. I was working with that function, so it's different now.










share|improve this question















Please, correct me with anything that I say in here (The actual question is below the code).



I've been quickly prototyping a project (ASP.NET Core 2.1) and haven't found the need to structure it with Webpack/Parcel/Rollup/Browserify as it's done with simple JavaScript validations per views (I do minify the files with Gulp).



Since I'm not enclosing my files into modules, they're all open to the global scope (bad idea, right?). Therefore, I've decided to "namespace" them by running everything inside a JavaScript variable.



The code that I have looks like this:



const formValidation = {
submitBtn : null as unknown as HTMLButtonElement,
errors: null as unknown as string,
errorsDiv: null as unknown as HTMLDivElement,
gradeSections: null as unknown as HTMLDivElement,
form: null as unknown as HTMLFormElement,
mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
this.errorsDiv = document.getElementById('js-errors') as HTMLDivElement;
this.gradeSections = document.getElementById('js-grade-sections') as HTMLDivElement;
this.form = document.getElementById('js-submit-form') as HTMLFormElement;
this.mapEventListenersFormValidation();
},
mapEventListenersFormValidation() {
this.submitBtn.addEventListener('click', this.submit.bind(this));
},
submit(evt: Event) {
console.log(evt);
evt.preventDefault();
this.resetForms();
if (!this.checkIfSubmitIsValid()) {
this.showErrors();
return;
}
this.form.submit();
},
checkIfSubmitIsValid() {
this.errors = ;
const subjectSelected = this.isSubjectSelected();
const sectionsSelected = this.areSectionsSelected();

if (!subjectSelected) {
selectSubjectDropdown.classList.add('is-invalid');
this.errors.push('Debe de seleccionar por lo menos una materia');
}

if (!sectionsSelected) {
this.gradeSections.classList.add('form-error');
this.errors.push('Debe de seleccionar al menos una sección');
}

return subjectSelected && sectionsSelected;
},
isSubjectSelected() {
return selectSubjectDropdown.selectedIndex !== 0;
},
areSectionsSelected() {
return selectedSectionCounter > 0;
},
showErrors() {

if (!this.errorsDiv || !this.errorsDiv.parentElement) {
return;
}
this.errorsDiv.parentElement.removeAttribute('hidden');
this.errorsDiv.innerHTML = '';
this.errors.forEach((error) => {
const li = document.createElement('li');
li.innerHTML = error;
this.errorsDiv.appendChild(li);
});
},
resetForms() {
selectSubjectDropdown.classList.remove('is-invalid');
this.gradeSections.classList.remove('form-error');
},

};

DOMReady(formValidation.mapDOM.bind(formValidation));


Check the submitBtn, errors, and errorsDiv properties. AFAIK, JavaScript doesn't have a way to declare an "empty" property. Therefore something sane would be to assign it either undefined or null.



As I want to have it statically defined (through TypeScript), I want to assign a type to those properties. The problem is that I just can't assign a non-nullable type to a null.



As I'm trying to avoid non-implicit any's in the scripts, the compiler suggested me to use unknown.



The question is, whether it's ok for me to use the unknown type from TypeScript to cast a null to an unknown so it can let me cast it to the type that I'm going after.



Edit Ended up adding the whole document into it. I was working with that function, so it's different now.







validation dom typescript null type-safety






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 2 at 0:08

























asked Oct 31 at 20:20









Jose A

1436




1436





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • The code to be reviewed looks sketchy or incomplete: where are checkIfSubmitIsValid() and showErrors() defined?
    – 200_success
    Nov 1 at 14:14










  • @200_success: Oh they're not needed for what I'm looking for. It's the second and third line of the code snippet above.
    – Jose A
    Nov 1 at 23:44










  • Maybe they're not needed for what you are looking for, but context is still important for reviewers.
    – 200_success
    Nov 1 at 23:55










  • @200_success: Got it! I'll update it asap! Sorry
    – Jose A
    Nov 2 at 0:07


















  • The code to be reviewed looks sketchy or incomplete: where are checkIfSubmitIsValid() and showErrors() defined?
    – 200_success
    Nov 1 at 14:14










  • @200_success: Oh they're not needed for what I'm looking for. It's the second and third line of the code snippet above.
    – Jose A
    Nov 1 at 23:44










  • Maybe they're not needed for what you are looking for, but context is still important for reviewers.
    – 200_success
    Nov 1 at 23:55










  • @200_success: Got it! I'll update it asap! Sorry
    – Jose A
    Nov 2 at 0:07
















The code to be reviewed looks sketchy or incomplete: where are checkIfSubmitIsValid() and showErrors() defined?
– 200_success
Nov 1 at 14:14




The code to be reviewed looks sketchy or incomplete: where are checkIfSubmitIsValid() and showErrors() defined?
– 200_success
Nov 1 at 14:14












@200_success: Oh they're not needed for what I'm looking for. It's the second and third line of the code snippet above.
– Jose A
Nov 1 at 23:44




@200_success: Oh they're not needed for what I'm looking for. It's the second and third line of the code snippet above.
– Jose A
Nov 1 at 23:44












Maybe they're not needed for what you are looking for, but context is still important for reviewers.
– 200_success
Nov 1 at 23:55




Maybe they're not needed for what you are looking for, but context is still important for reviewers.
– 200_success
Nov 1 at 23:55












@200_success: Got it! I'll update it asap! Sorry
– Jose A
Nov 2 at 0:07




@200_success: Got it! I'll update it asap! Sorry
– Jose A
Nov 2 at 0:07










1 Answer
1






active

oldest

votes

















up vote
0
down vote













The compiler can't know beforehand if document.getElementById('js-btn-submit') wil return anything. That is why you cannot assign the result directly to a type.



One solution is to tell the compiler that you are certain that the HTML element exists, using !



let button = document.getElementById("button")! as HTMLButtonElement


If you are NOT certain that the button exists in the HTML document, you can use the suggestion given by VS Code:



If you hover the mouse over document.getElementById you can see that the returned type is null | HTMLElement



I've rewritten your object as a class, because in object notation the colon is used to pass the value, not the type: const obj = {a:3}



class FormValidation {
submitBtn: null | HTMLButtonElement

mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
}
}

const f = new FormValidation()
f.mapDOM()





share|improve this answer























  • Thanks Kokodoko, the only thing is that the compiler is now complaining with "null" by saying that "Object is possibly null", and that "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". I've also checked that null as unkown | HTMLFormElement works as well!
    – Jose A
    Nov 1 at 23:47










  • I see, this is because the colon : in object notation is used to pass the value, instead of the type! obj = {a:3}. So you can't define the type in this way. In my update I have rewritten the object as a class so you can define the type.
    – Kokodoko
    Nov 2 at 8:35










  • If it's a class it then does make sense! You even gave me an idea! To define the object's property at the object level and not at the property level!! Thanks :)
    – Jose A
    Nov 2 at 11:07











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206689%2ftypescript-3-form-validation-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













The compiler can't know beforehand if document.getElementById('js-btn-submit') wil return anything. That is why you cannot assign the result directly to a type.



One solution is to tell the compiler that you are certain that the HTML element exists, using !



let button = document.getElementById("button")! as HTMLButtonElement


If you are NOT certain that the button exists in the HTML document, you can use the suggestion given by VS Code:



If you hover the mouse over document.getElementById you can see that the returned type is null | HTMLElement



I've rewritten your object as a class, because in object notation the colon is used to pass the value, not the type: const obj = {a:3}



class FormValidation {
submitBtn: null | HTMLButtonElement

mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
}
}

const f = new FormValidation()
f.mapDOM()





share|improve this answer























  • Thanks Kokodoko, the only thing is that the compiler is now complaining with "null" by saying that "Object is possibly null", and that "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". I've also checked that null as unkown | HTMLFormElement works as well!
    – Jose A
    Nov 1 at 23:47










  • I see, this is because the colon : in object notation is used to pass the value, instead of the type! obj = {a:3}. So you can't define the type in this way. In my update I have rewritten the object as a class so you can define the type.
    – Kokodoko
    Nov 2 at 8:35










  • If it's a class it then does make sense! You even gave me an idea! To define the object's property at the object level and not at the property level!! Thanks :)
    – Jose A
    Nov 2 at 11:07















up vote
0
down vote













The compiler can't know beforehand if document.getElementById('js-btn-submit') wil return anything. That is why you cannot assign the result directly to a type.



One solution is to tell the compiler that you are certain that the HTML element exists, using !



let button = document.getElementById("button")! as HTMLButtonElement


If you are NOT certain that the button exists in the HTML document, you can use the suggestion given by VS Code:



If you hover the mouse over document.getElementById you can see that the returned type is null | HTMLElement



I've rewritten your object as a class, because in object notation the colon is used to pass the value, not the type: const obj = {a:3}



class FormValidation {
submitBtn: null | HTMLButtonElement

mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
}
}

const f = new FormValidation()
f.mapDOM()





share|improve this answer























  • Thanks Kokodoko, the only thing is that the compiler is now complaining with "null" by saying that "Object is possibly null", and that "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". I've also checked that null as unkown | HTMLFormElement works as well!
    – Jose A
    Nov 1 at 23:47










  • I see, this is because the colon : in object notation is used to pass the value, instead of the type! obj = {a:3}. So you can't define the type in this way. In my update I have rewritten the object as a class so you can define the type.
    – Kokodoko
    Nov 2 at 8:35










  • If it's a class it then does make sense! You even gave me an idea! To define the object's property at the object level and not at the property level!! Thanks :)
    – Jose A
    Nov 2 at 11:07













up vote
0
down vote










up vote
0
down vote









The compiler can't know beforehand if document.getElementById('js-btn-submit') wil return anything. That is why you cannot assign the result directly to a type.



One solution is to tell the compiler that you are certain that the HTML element exists, using !



let button = document.getElementById("button")! as HTMLButtonElement


If you are NOT certain that the button exists in the HTML document, you can use the suggestion given by VS Code:



If you hover the mouse over document.getElementById you can see that the returned type is null | HTMLElement



I've rewritten your object as a class, because in object notation the colon is used to pass the value, not the type: const obj = {a:3}



class FormValidation {
submitBtn: null | HTMLButtonElement

mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
}
}

const f = new FormValidation()
f.mapDOM()





share|improve this answer














The compiler can't know beforehand if document.getElementById('js-btn-submit') wil return anything. That is why you cannot assign the result directly to a type.



One solution is to tell the compiler that you are certain that the HTML element exists, using !



let button = document.getElementById("button")! as HTMLButtonElement


If you are NOT certain that the button exists in the HTML document, you can use the suggestion given by VS Code:



If you hover the mouse over document.getElementById you can see that the returned type is null | HTMLElement



I've rewritten your object as a class, because in object notation the colon is used to pass the value, not the type: const obj = {a:3}



class FormValidation {
submitBtn: null | HTMLButtonElement

mapDOM() {
this.submitBtn = document.getElementById('js-btn-submit') as HTMLButtonElement;
}
}

const f = new FormValidation()
f.mapDOM()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 2 at 8:40

























answered Nov 1 at 11:37









Kokodoko

1967




1967












  • Thanks Kokodoko, the only thing is that the compiler is now complaining with "null" by saying that "Object is possibly null", and that "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". I've also checked that null as unkown | HTMLFormElement works as well!
    – Jose A
    Nov 1 at 23:47










  • I see, this is because the colon : in object notation is used to pass the value, instead of the type! obj = {a:3}. So you can't define the type in this way. In my update I have rewritten the object as a class so you can define the type.
    – Kokodoko
    Nov 2 at 8:35










  • If it's a class it then does make sense! You even gave me an idea! To define the object's property at the object level and not at the property level!! Thanks :)
    – Jose A
    Nov 2 at 11:07


















  • Thanks Kokodoko, the only thing is that the compiler is now complaining with "null" by saying that "Object is possibly null", and that "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". I've also checked that null as unkown | HTMLFormElement works as well!
    – Jose A
    Nov 1 at 23:47










  • I see, this is because the colon : in object notation is used to pass the value, instead of the type! obj = {a:3}. So you can't define the type in this way. In my update I have rewritten the object as a class so you can define the type.
    – Kokodoko
    Nov 2 at 8:35










  • If it's a class it then does make sense! You even gave me an idea! To define the object's property at the object level and not at the property level!! Thanks :)
    – Jose A
    Nov 2 at 11:07
















Thanks Kokodoko, the only thing is that the compiler is now complaining with "null" by saying that "Object is possibly null", and that "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". I've also checked that null as unkown | HTMLFormElement works as well!
– Jose A
Nov 1 at 23:47




Thanks Kokodoko, the only thing is that the compiler is now complaining with "null" by saying that "Object is possibly null", and that "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". I've also checked that null as unkown | HTMLFormElement works as well!
– Jose A
Nov 1 at 23:47












I see, this is because the colon : in object notation is used to pass the value, instead of the type! obj = {a:3}. So you can't define the type in this way. In my update I have rewritten the object as a class so you can define the type.
– Kokodoko
Nov 2 at 8:35




I see, this is because the colon : in object notation is used to pass the value, instead of the type! obj = {a:3}. So you can't define the type in this way. In my update I have rewritten the object as a class so you can define the type.
– Kokodoko
Nov 2 at 8:35












If it's a class it then does make sense! You even gave me an idea! To define the object's property at the object level and not at the property level!! Thanks :)
– Jose A
Nov 2 at 11:07




If it's a class it then does make sense! You even gave me an idea! To define the object's property at the object level and not at the property level!! Thanks :)
– Jose A
Nov 2 at 11:07


















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206689%2ftypescript-3-form-validation-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei