Combination of values in object attributes typescript

I was wondering if there is a construct in typescript that I can use in order to restrict possible values of an object depending on the values. I can describe it something like that: interface IObject = { key1:string; key2:string; key3:"A" | null; key4: (if key3 is ‘A’ then allow ‘value1′ |’value2’ else if it… Read More Combination of values in object attributes typescript

How can I declare an array of unknown elements where the first one must be a string?

I’m trying to write a function that accepts an array of arrays. The inner array can have any number of elements of any type, but the first one must be a string. The following is a valid input array: [ [‘2022-03-04T00:00:00Z’, 64, 10], [‘2022-03-05T00:00:00Z’, 61, 12], [‘2022-03-06T00:00:00Z’, 66, 14], ] But the following is not… Read More How can I declare an array of unknown elements where the first one must be a string?

How to fix "Type 'string | boolean' is not assignable to type 'never'. Type 'string' is not assignable to type 'never'"?

interface IData { title: string; slug: string; published_at: string; isPopular: boolean; } const product: IData = { title: "Title 1", slug: "title-1", published_at: "2012", isPopular: true, }; function getCourseBySlug(fields: Array<keyof IData>) { const items = <IData>{}; // // Ensure only the minimal needed data is exposed fields.forEach((field) => { if (field === "isPopular") { items[field]… Read More How to fix "Type 'string | boolean' is not assignable to type 'never'. Type 'string' is not assignable to type 'never'"?

How to pass typescript property to bootstrap related jQuery function in Angular Application

this.addRoleFormGroup.dirty; //want to pass this value to jQuery on("hide.bs.modal", function()) this.addRoleFormGroup.dirty; $("#addRoleModal").on("hide.bs.modal", function () { alert(‘The modal is about to be hidden.’); $(‘#addRoleModal’).modal(‘show’); }); } >Solution : Use an arrow function for your JQuery event handler. That way the outer this will be accessible from within the callback: $("#addRoleModal").on("hide.bs.modal", () => { console.log(this.addRoleFormGroup.dirty); $(‘#addRoleModal’).modal(‘show’); });… Read More How to pass typescript property to bootstrap related jQuery function in Angular Application

TypeScript: Check if variable has been initialised

I have some function as follows: const myFunction = (columns: any, data: any) => { let label: string; let value: number; //some section where it assigns label and value for (let index = 0; index < columns.length; index++) { const column: string = columns[index].fieldName; const datapoint: string = data[index].formattedValue; if (column.includes("KeyPointLabel")) { label = datapoint;… Read More TypeScript: Check if variable has been initialised

Fetch data changing on reload from array to undefined

I’m getting some weird error. Just starting with typescript. When i write console.log(data) for the first time i get an array with objects inside. The problem occurs when i reload the webpage without touching anything. My console log returns undefined. const Home = (): JSX.Element => { const [data, setData] = useState<any>(); const [loading, setLoading]… Read More Fetch data changing on reload from array to undefined