Take a look at this Typescript snippet:
let action = function (): void {
//perform actions
};
let result = action();
What makes it suitable for the TypeScript compiler?
Take a look at this Typescript snippet:
let action = function (): void {
//perform actions
};
let result = action();
What makes it suitable for the TypeScript compiler?
It seems that the reason for this behavior is due to the fact that in Typescript, void
is considered a distinct type with specific values like undefined
and null
, allowing it to be used in assignments.
The potential risk of using values of type void
in assignments is concerning as it could result in serious errors. For instance, if a function originally designed to return a meaningful value is changed to return void
, there may not be any compile-time error generated.
According to the Typescript 1.4 language specification, the Void type signifies the absence of a value, exclusively accepting null and undefined as its possible values. While Void intersects with Any and encompasses Null and Undefined, its correlation with other types is non-existent.
NOTE: Although declaring variables of type Void may seem unnecessary, their usage becomes inevitable when dealing with generic types or functions.
What is the reasoning behind allowing void functions in the left side of an assignment in TypeScript?
This allowance is simply because there are no explicit restrictions against it. However, it should be noted that using a void function on the left side doesn't have much practical utility as demonstrated in the example below:
var action = function action(): void {};
var result = action();
result.bar; // Error
Take a look at this code snippet:
// Function to measure performance
function time<T>(f: () => T): T {
var start = Date.now();
var result = f();
console.log('It took ' + (Date.now() - start) + ' to run');
return f;
}
function doSomething(): void { }
time(doSomething);
If we consider generic functions as specific instances of a template based on type (note: this is not accurate), the variable result
will have a type of void
when time
is called with doSomething
. Despite this, there are no catastrophic consequences when dealing with a variable of type void
, so it's not imperative to prevent its usage.
Another example:
declare function doNothing(): void;
declare function doOneThing(): void;
declare function doManyThings(): void;
function doSomething(x: number) {
switch (x) {
case 0:
return doNothing();
case 1:
return doOneThing();
default:
return doManyThings();
}
}
In this scenario, if we modify the return type of doSomething
, we must also update the return types of doNothing
, doOneThing
, and doManyThings
. Once again, we can observe that a void
value can be handled without issues. This example is not contrived; it is based on actual TypeScript compiler code.
Let's make some tweaks to the code:
function doSomething(x: number) {
if (x > 1) {
return doManyThings();
} else {
// result: void!
var result = x === 0 ? doNothing() : doOneThing();
return result;
}
}
Why should such a harmless refactoring trigger an error? What purpose does it serve? If we attempt to access properties of result
, it will lead to an error. In cases where we do not need to access any properties, we are likely following correct practices and should not face any issues.
How should SectionList be properly typed? I am encountering an issue where this code works (taken from the official documentation example): <SectionList renderItem={({item, index}) => <Text key={index}>{item}</Text>} renderSectionHea ...
Looking for guidance on how to use TypeScript interface to define these C# models: public class PageModel { public long Id { get; set; } public string Name { get; set; } public IDictionary<string, FieldModel> Fields { get; set; } } pu ...
Hey everyone, I'm facing an issue and need some advice. I prefer using interfaces to maintain readability and utilize intellisense in my code. However, I'm struggling with implementing this approach when working with data passed through props. H ...
Lately, I've been in the process of upgrading my Angular version from 10 to 13 in order to utilize TypeScript 4.6. However, during this upgrade, I made some errors with types in my tests and I'm wondering if others have encountered similar issues ...
Working on implementing lazy loading for my Angular 4 application, I have a total of 18 lazy loaded modules. Upon noticing that fetching these modules is taking some time, I decided to add a loading indicator. Everything worked fine when I added it locally ...
In my application, I have a parent component called 'EmployeeComponent' that is responsible for displaying a list of employees. Additionally, there is a child component named 'EmployeeDetailComponent' which displays the details of the s ...
I often encounter situations where I need to select a specific entry from a set of data in a component, whether through a select box or as part of a table. However, the way I intend to utilize this data typically requires additional fields like the "label ...
Whenever I attempt to send this dispatch, it consistently results in the following error message (and the same issue occurs with all my other dispatches): The argument of type '(dispatch: Dispatch) => Promise' is not compatible with a paramet ...
Whenever I attempt to access a property of the object named "User," it shows up as undefined. However, upon logging the complete object to the console, the field appears with the necessary data. Here is the console log output: perfil.component.ts:42 unde ...
I'm exploring the distinction between import and require in relation to exporting and importing mutable values. Picture a file a.ts: export let a = 1; export function f() { a = 2; } Next, we have three versions of a main file, index1.ts: import { ...
I am currently developing a .net Core 2.2/Angular 8 application and recently came across the HealthCheck feature. I decided to incorporate it into my application, so here is a snippet from my Startup.cs file: using HealthChecks.UI.Client; using Mi ...
I am faced with the following scenario: public async handle( handler: WorkflowHandlerOption, payload: <how_to_type_it?>, ): Promise<StepResponseInterface> { switch (handler) { case WorkflowHandlerOption.JOB_APPLICATION_ACT ...
One of the methods in my Angular component is responsible for returning data Here is a snippet of that method getRecognitionById() { this.loaderService.show(null, true); forkJoin( this.vendorWebApiService.getRecognitionById(this.executiveCh ...
Having 4 schemas in this example, namely Picture, Video, and Game, where each can have multiple Download instances. While this setup works well when searching downloads from the invoker side (Picture, Video, and Game), it becomes messy with multiple tables ...
After coming across a SSR demo (React+typescript+Next.js) that utilizes two plugins, I found myself wondering why exactly it needs both of them. In my opinion, these two plugins seem to serve the same purpose. Can anyone provide insight as to why this is? ...
I attempted to follow a tutorial to set up Angular 2 with TypeScript from the following link: https://angular.io/guide/quickstart However, I encountered the following error: ReferenceError: System is not defined System.config I am uncertain why this e ...
When running my calculatePercents() method, I am receiving NULL% instead of 0%. Upon checking my console.log, I noticed that the values being printed are shown as NULL. calculatePercents() { this.v.global.total.amount = this.v.global.cash.amount + ...
Let me simplify my issue: I am currently using NgxDatatable to display a CRUD table. I have a base component named CrudComponent, which manages all CRUD operations. This component was designed to be extended for all basic entities. The challenge I am en ...
I implemented a listener for the 'cuechange' event on a Text Track and it's functioning correctly. However, I am unable to figure out how to remove this listener. I have attempted the instructions below to remove the listener, but it continu ...
Currently, I am working with @reduxjs/toolkit and aiming to develop a function that can easily create a slice with default reducers. Although my current implementation is functional, it lacks strong typing. Is there a way to design a function in such a man ...