I've been creating a form structure that's functioning smoothly, but I've encountered an interesting issue with field validation in the validation part.
While my Visual Code is pointing out that 'required2' in the phone
constant needs to be changed to 'required', the phonefield item within the fields collection of the form
constant doesn't raise any complaints about using 'required2'. It only becomes invalid when the interface shows 'required' instead of 'required2'.
My objective is to limit validation on a per-form-field basis to prevent unauthorized additions to the validation dictionary.
It's worth noting that changing 'phonenumber' to 'phonenumber2' results in incorrect typing, while altering the 'required' key doesn't affect it since it isn't necessary.
Here are my interfaces:
declare type fields = IPhoneField | ISelectField ..etc;
export interface IPage {
title?: string;
buttons?: IButton[];
fields: fields[];
showOn?: IDictionary<string | number>;
}
export interface IForm {
modelPreset: IDictionary<string | number>;
successPageConfig?: IDictionary<string | number | boolean>;
presentation: pagePresentation;
pages: IPage[];
}
interface IField {
label: string;
model: string;
placeholder?: string;
addon?: string;
showOn?: IDictionary<string | number>;
maxlength?: number;
}
export interface IPhoneField extends IField {
type: 'phone';
validation: {
required?: string;
phonenumber: string;
};
}
And here's my code snippet:
const phone: IPhoneField = {
"label":"Phone Number",
"model":"phonenumber",
"type":"phone",
"placeholder":"Phone number",
"validation":{
"required2":"A phone number is required",
"phonenumber":"Please enter a valid phone number",
}
};
const form: IDictionary<IForm> = {
"form1":{
"pages":[
{
"fields": [
{
"label":"Phone Number",
"model":"phonenumber",
"type":"phone",
"placeholder":"Phone number",
"validation":{
"required2":"A phone number is required",
"phonenumber":"Please enter a valid phone number"
}
}
]
}
]
}
};
Has anyone else experienced this issue? Could it possibly be a bug with Typescript?
Check out the playground link for more details: NEW playground