Crafting Structured Data with Template Literal Types
Template literal types in TypeScript allow you to create new types by interpolating values into string literals. This is incredibly useful for creating structured data types, like generating types for API responses or configuration objects, based on a predefined schema. This challenge will test your understanding of how to leverage template literal types to build these structured types.
Problem Description
You are tasked with creating a TypeScript function createDataStructure that generates a type representing a structured data object based on a provided schema. The schema will be an array of strings, where each string represents a property name and its corresponding type. The function should use template literal types to construct a type where each property name is wrapped in backticks and its type is inferred from the schema string.
What needs to be achieved:
- The function
createDataStructureshould accept an array of strings representing the schema. - It should return a type that is an object with properties whose names are the strings from the schema, wrapped in backticks, and whose types are
string. - The resulting type should be structurally equivalent to an object with properties named after the schema strings, each of type
string.
Key Requirements:
- Utilize template literal types to dynamically create the property names.
- Ensure the resulting type is an object.
- Each property's type should be
string.
Expected Behavior:
When called with a schema like ["name", "age", "city"], the function should return a type equivalent to:
{
`name`: string;
`age`: string;
`city`: string;
}
Edge Cases to Consider:
- Empty schema array: Should return an empty object type
{}. - Schema array with duplicate property names: The last occurrence of a property name in the schema should determine the final type.
- Schema array with non-string elements: While not explicitly required, consider how your solution handles this (e.g., ignoring non-string elements).
Examples
Example 1:
Input: ["name", "age", "city"]
Output: { `name`: string; `age`: string; `city`: string; }
Explanation: The function constructs an object type with three properties: 'name', 'age', and 'city', each of type 'string'.
Example 2:
Input: []
Output: {}
Explanation: An empty schema array results in an empty object type.
Example 3:
Input: ["name", "age", "name"]
Output: { `name`: string; `age`: string; }
Explanation: Duplicate property names are handled by using the last occurrence in the schema.
Constraints
- The input schema will be an array of strings.
- The output must be a valid TypeScript type.
- The function should be performant enough to handle schemas with up to 100 properties. (This is more of a guideline than a strict constraint).
Notes
- Think about how to use template literal types to dynamically create the property names.
- Consider using a
for...ofloop ormapfunction to iterate over the schema array. - The goal is to create a type, not a runtime object. You're defining the shape of an object, not creating an instance of it.
- TypeScript's type inference can be helpful here.