Dec. 13, 2023, 3 p.m. | Nhan Nguyen

DEV Community dev.to


Be Specific with Allowed Types


We will validate forms again in this example.



const Form = z.object({
repoName: z.string(),
privacyLevel: z.string(),
})

export const validateFormInput = (values: unknown) => {
const parsedData = Form.parse(values);

return parsedData;
}


This time Form has a privacyLevel that can be two types: either private or public:



const Form = z.object({
repoName: z.string(),
privacyLevel: z.string(),
});


If we were going to represent this in TypeScript, we would write:



type PrivacyLevel = 'private' …

declaration export forms javascript library object programming return types typescript validation

SOC 2 Manager, Audit and Certification

@ Deloitte | US and CA Multiple Locations

Senior InfoSec Manager - Risk and Compliance

@ Federal Reserve System | Remote - Virginia

Security Analyst

@ Fortra | Mexico

Incident Responder

@ Babcock | Chester, GB, CH1 6ER

Vulnerability, Access & Inclusion Lead

@ Monzo | Cardiff, London or Remote (UK)

Information Security Analyst

@ Unissant | MD, USA