cboard-org / cboard-ai-engine

This engine powers the Cboard AI builder, designed to generate content suggestions for communication boards and create new pictograms as necessary. With a simple prompt, it will generate a list of suggestions that can be used to create an AAC board
https://cboard.io
GNU General Public License v3.0
1 stars 1 forks source link

Add string templates to the different types. #5

Open RodriSanchez1 opened 9 months ago

RodriSanchez1 commented 9 months ago

In typescript there's a way to define string template type. I'll search later and show an example.

Edit: from ChatGPT

In TypeScript, you can define a type for a string template using template literal types, which were introduced in TypeScript 4.1. Template literal types allow you to model specific string patterns and can be very useful for a variety of use cases, such as defining types for specific formatting patterns, routes, or any other string-based API.

Here’s how you can define a type for a string template:

Basic Example

type MyTemplateType = `prefix-${string}`;

This type accepts any string that starts with prefix- followed by any other string.

More Specific Example

If you want to be more specific about the part that comes after the prefix, you can do so. For example, to define a type for a date string template:

type DateString = `${number}-${number}-${number}`;

This type matches strings that resemble a date in the format YYYY-MM-DD.

Combining with Union Types

You can also combine template literal types with union types to define a type that accepts multiple specific formats:

type SpecificFormat = `data-${'A' | 'B' | 'C'}-${number}`;

This type would accept strings like data-A-1, data-B-100, or data-C-24, where the second part is limited to 'A', 'B', or 'C', followed by a numeric value.

Using with Functions

You can use these types in functions to ensure the inputs match the desired pattern:

function processTemplate(input: SpecificFormat) {
  // Function implementation
}

// This will be accepted
processTemplate("data-A-123");

// This will cause a TypeScript error
processTemplate("wrong-format-123");

Template literal types are very powerful for creating more descriptive and restrictive type definitions based on string patterns, improving code reliability and developer experience by catching potential mismatches at compile time.

_Originally posted by @shayc in https://github.com/cboard-org/cboard-ai-engine/pull/1#discussion_r1504531625_

shayc commented 9 months ago

I wouldn't sweat over it. Chat gpt can convert global symbols swagger json schema to typescript.