Hone logo
Hone
Problems

Simple JavaScript Type Checker

This challenge asks you to build a basic type checker for JavaScript. Type checking is a crucial part of software development, helping to catch errors early and improve code reliability. Your type checker will analyze JavaScript expressions and verify that they adhere to specified type annotations.

Problem Description

You are tasked with creating a function typeCheck that takes a JavaScript expression and a type annotation as input and determines if the expression's type matches the annotation. The expression will be represented as a JavaScript object with a value and a type property. The type annotation will also be a JavaScript object with a type property.

The supported types are:

  • "number": Represents numeric values.
  • "string": Represents string values.
  • "boolean": Represents boolean values (true or false).
  • "null": Represents the null value.
  • "undefined": Represents the undefined value.
  • "array": Represents an array. The array's elements should all be of the same type.
  • "object": Represents a JavaScript object. For simplicity, assume all object values are valid.

The value property of the expression object will hold the actual JavaScript value. The type property of the expression object will be a string representing the type of the value.

The type property of the type annotation object will specify the expected type.

Your typeCheck function should return true if the expression's type matches the type annotation, and false otherwise.

Key Requirements:

  • Handle all supported types correctly.
  • For arrays, ensure all elements are of the same type.
  • For objects, simply return true (no need to validate object properties).
  • Handle null and undefined correctly.

Expected Behavior:

The function should accurately determine if the expression's type matches the annotation. It should return true for a match and false for a mismatch.

Edge Cases to Consider:

  • Empty arrays.
  • Arrays with mixed types (should return false).
  • Null and undefined values.
  • Invalid type strings (should be handled gracefully, potentially returning false).

Examples

Example 1:

Input: { value: 123, type: "number" }, { type: "number" }
Output: true
Explanation: The expression's value is a number, and the annotation specifies a number type.

Example 2:

Input: { value: "hello", type: "string" }, { type: "number" }
Output: false
Explanation: The expression's value is a string, but the annotation specifies a number type.

Example 3:

Input: { value: [1, 2, 3], type: "array" }, { type: "array" }
Output: true
Explanation: The expression's value is an array of numbers, and the annotation specifies an array type.

Example 4:

Input: { value: [1, "a", 3], type: "array" }, { type: "array" }
Output: false
Explanation: The expression's value is an array with mixed types (number and string), which is invalid.

Example 5:

Input: { value: null, type: "null" }, { type: "null" }
Output: true
Explanation: The expression's value is null, and the annotation specifies a null type.

Example 6:

Input: { value: undefined, type: "undefined" }, { type: "undefined" }
Output: true
Explanation: The expression's value is undefined, and the annotation specifies an undefined type.

Constraints

  • The value property of the expression object can be any valid JavaScript value.
  • The type property of both the expression and annotation objects will be strings.
  • Array elements will be JavaScript values.
  • The function must handle all supported types (number, string, boolean, null, undefined, array, object).
  • The function should not throw errors; it should return true or false.

Notes

  • Consider using typeof to determine the type of JavaScript values. However, be aware of its limitations (e.g., typeof null returns "object").
  • For arrays, you'll need to iterate through the elements and check their types.
  • This is a simplified type checker; it doesn't cover all aspects of JavaScript typing. Focus on the core requirements outlined above.
  • Think about how to handle unexpected or invalid type strings gracefully. Returning false is a reasonable approach.
Loading editor...
javascript