why doesn't typescript accept if(Array.length) for differentiating between an object and array

I recently started using typescript and I’ve run into a confusing little part, where I don’t quite get why typescript behaves like it does. (Involving typechecking a parameter that could either be an array or an object) I first tried checking as the upper example shows, but it didn’t work (Typescript didn’t like me calling… Read More why doesn't typescript accept if(Array.length) for differentiating between an object and array

Const array using data from two objects in TypeScript

I require some help on this particular issue I’m encountering, I’m trying to const an array using two objects with Object.keys(…).map and &&. Code: const plugins: PluginManifest[] = Object.values(window.Aliucord.pluginManager.plugins).map((p) => p.manifest) && Object.values(window.Aliucord.pluginManager.disabledPlugins).map(p => p); The above code uses the data only from the disabledPlugins Object. Am I doing something wrong or is there a… Read More Const array using data from two objects in TypeScript

React component does not re-render when I use a component for building forms

So I have this component which is used for forms handling: import React, { useState } from "react"; export const useForm = (callback: any, initialState = {}) => { const [values, setValues] = useState(initialState); const onChange = (event: React.ChangeEvent<any>) => { setValues({ …values, [event.target.name]: event.target.value }); }; const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {… Read More React component does not re-render when I use a component for building forms

Typescript: pass interface as arguments of function signature

I have an interface where I define an onChange function interface SimpleInterface { onChange: (fieldId: FieldId, column: number) => void; } I’d like to pass an interface as the arguments, something like: interface Arguments { fieldId: FieldId, column: number } But this won’t work: onChange: (Arguments) => void; I’m getting the error ‘parameter has a… Read More Typescript: pass interface as arguments of function signature

Why does typescript not recognise my variable is an array after checking function?

I have a simple checking function for array length: export const hasLength = (arr?: unknown[]) => { const isArray = Array.isArray(arr); return isArray && arr.length !== 0; }; I’d expect typescript to recognise that the variable is an array with length after checking through the function but it doesn’t. Is this expected behaviour and why… Read More Why does typescript not recognise my variable is an array after checking function?

How to simplify simple statement in typescript or js

I would like to simplify this expression but don’t know how. There has to be a way to have the same expression without the three conditions. if ( request.Document != null && request.Document != undefined && request.Document != "" ) Just for context I am talking something similar to c#’s !string.isNullOrEmpty() >Solution : if (request.Document)… Read More How to simplify simple statement in typescript or js