Sending Status:
const statusArray = ["confirmed", "pending", "canceled"]
Purpose: While the type is automatically generated, I also require it to be in array form.
Sending Status:
const statusArray = ["confirmed", "pending", "canceled"]
Purpose: While the type is automatically generated, I also require it to be in array form.
Extracting a runtime value from a TypeScript type is not feasible (but the reverse is possible). A workaround is to set up alerts in case you overlook including a member of status
. Here are 3 strategies I employ, each with its own advantages and drawbacks.
Approach 1: leveraging permutations. This method works well for small status
types with 2-5 members but becomes sluggish with larger unions. However, it's straightforward and only requires an explicit type annotation, while also preventing duplicates.
type Permutations<T extends string> = [T] extends [never] ? [] : {
[K in T]: [K, ...Permutations<Exclude<T, K>>];
}[T];
type StatusArray = Permutations<status>;
const statusArray: StatusArray = ["confirmed", "pending", "canceled"];
Approach 2: using a function. This approach accommodates all union sizes and provides detailed error messages. The downside is the need for a cumbersome function call.
function createStatusArray<T extends status>(array: (status extends T ? T : { error: `Missing '${Exclude<status, T>}' from status` })[]) { return array; }
const statusArray = createStatusArray(["confirmed", "pending", "canceled"]);
Approach 3: employing an assertion. Similar to Method 2, this method handles all union sizes but might lack clarity in error messaging. Nevertheless, it triggers an error if not all status
members are included, although it necessitates an additional variable declaration for the mutable array type.
const _statusArray = ["confirmed", "pending", "canceled"] as const;
type AssertStatusArray<T extends never = Exclude<status, typeof _statusArray[number]>> = T;
const statusArray = [..._statusArray];
As mentioned by another user, the approach you are attempting is not feasible in this case. However, you can achieve the desired outcome by starting with the concrete array and using as const
as shown below:
const statusArray = ["confirmed", "pending", "canceled"] as const;
type StatusType = typeof statusArray[number];
const myStatus: StatusType = "confirmed";
const myOtherStatus: StatusType = "non_status";
// Type '"non_status"' is not assignable to type '"confirmed" | "pending" | "canceled"'.(2322)
If you want to ensure that the statusArray
contains all elements, one approach is to create a helper function for this validation:
function checkAllStatusPresent<Type>() {
return function <T extends Type[]>(
...types: [Type] extends [T[number]] ? T : never
) {
return types
}
}
const checkAllStatusPresent = checkAllAppear<status>()
const statusArray = checkAllStatusPresent("confirmed", "pending", "canceled")
Although these functions do not perform any operation, if certain elements are missing in the arguments (e.g.
checkAllStatusPresent("pending", "canceled")
), it will result in a type error with the argument type becoming never
.
Is there a way to connect ngModel values with select-searchable options in Ionic so that default values from localStorage are displayed? <ion-col col-6> <select-searchable okText="Select" cancelText="Cancel" cla ...
I am facing an issue with a class: export class TestClass { paymentDate: Date; } Whenever I retrieve an object of this class from a server API, the paymentDate field comes as a string instead of a Date object. This prevents me from calling the ...
Has anyone figured out how to resolve this issue? .ts this.router.navigate(["", { clientId: data.id }]) Error message { path: "", component: HomePage, }, An unhandled error occurred: Root segme ...
When I initiate a Patch Request from the frontend, it takes approximately 30-40 seconds for the backend to resolve. const handleSendClick = (data: any) => { const requiredLanguages = Array.isArray(data.required_languages) ? data.required_langu ...
I'm currently working on a small Form using the kit feature Actions. However, I'm facing an issue when trying to submit the form - I keep receiving a "405 POST method not allowed. No actions exist for this page" error message. My code is quite st ...
Since beginning my project, I have encountered an issue that is both normal and frustrating. The dist folder is being created with incomplete information related to the components inside it. dashboard dist (unwanted) components panel dist (unwanted) c ...
Recently, I encountered a scenario where I have a TypeScript script called test.ts structured like this: class Foo { public static bar() { console.log("test"); } } The requirement was to call this TypeScript function from plain JavaScript ...
I've encountered an issue with my code. I created a feature that adds empty rows if there are less than 5 rows, but now the sort function is no longer functioning properly. Strangely, when I remove the for loop responsible for adding empty rows, the s ...
Displayed below is an element created using ngFor <span *ngFor="let picture of pictures; let i = index"> <a target="_blank" href="{{picture.image}}" class="thumbnail-display image-overlay"> <span class="overlay-icon hide"> ...
Is it possible to define an element that operates in the manner indicated here? function fn<T, U extends keyof T, T[U] extends number>() I am having trouble making the "T[U] extends number" portion function correctly. ...
Here's the TypeScript code I'm working with: let a: unknown = true; if(hasColour(a)) { console.log(a.colour); // Using a.colour after confirming a has the colour property } I've created a function to check if the color property exist ...
After spending two whole days trying to decipher the Heroku error message with no success, I'm still unable to pinpoint what is causing the issue. 2021-07-18T04:27:08.741998+00:00 app[web.1]: {"level":30,"time":1626582428741,&quo ...
Why is the arguments object showing a length of zero when I pass parameters to the function, even though I can still access its properties? I am referring to the arguments object that is implemented by all JavaScript functions, allowing you to access the f ...
When working with Java, I often use lombok to exclude certain fields from being printed. For instance, the @ToString.Exclude annotation can be used to prevent printing the user token. import lombok.ToString; public class TokenResponse { @ToString.Excl ...
Exploring the functionalities of Angularjs 2.0, I encountered an issue when attempting to inject a service into a class. Below is the code snippet that's causing trouble: import {Component, View, bootstrap, NgFor, HttpService, Promise} from 'ang ...
I'm currently in the process of building a CDK stack and I am fairly new to CDK. My goal is to create a Simple Email Service (SES) ConfigurationSet followed by an EmailIdentity. The issue I encountered is that the creation of the EmailIdentity fails d ...
Recently, I decided to delve into learning TypeScript by building a simple shopping cart application. If you want to check out the code, feel free to visit my GitHub repository: https://github.com/CsarGomez/shopping-cart-reducers-tx I've encountered ...
I am struggling with ternary operators in TypeScript and need help understanding the issue. Please review the code below: const QuizQuestionContainer = ({ qa }: QuizQuestionContainerPropsType) => { const { question, option1, option2, option ...
Is there a way to extract specific data from a JSON format within an Ionic project? What is the process for selecting the ID associated with particular data in a JSON format? And how can I retrieve and display the value linked to the selected product&apos ...
In my current array, the structure looks like this: const books[{ name: 'book1', id: '1', fromStatus: [{ name: 'Available', id: 1 }, { name: 'Free', id: 2 }], t ...