Skip to main content
deleted 88 characters in body
Source Link
Abraham
  • 16.2k
  • 12
  • 91
  • 124

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as Record<string,string>number>... that can cast it, instead of type assertion

HereHere is a link to the code on Typescript playground

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as Record<string,string>... that can cast it, instead of type assertion

Here is a link to the code on Typescript playground

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as Record<string,number>... that can cast it, instead of type assertion

Here is a link to the code on Typescript playground

added 348 characters in body
Source Link
Abraham
  • 16.2k
  • 12
  • 91
  • 124

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as Record<string,string>... that can cast it, instead of type assertion

Here is a link to the code on Typescript playground

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as Record<string,string>... that can cast it, instead of type assertion

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as Record<string,string>... that can cast it, instead of type assertion

Here is a link to the code on Typescript playground

deleted 24 characters in body
Source Link
Abraham
  • 16.2k
  • 12
  • 91
  • 124

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as numberRecord<string,string>... that can cast it, instead of type assertion

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as number...

I have a variable that has type of number or string. How can I assign its value to a variable that accepts number only?

const id:Record<string, string|number> = {name:3}
let num: Record<string, number>;

num = id; //Shows error here

I know there can be javascript work arounds like writing a conversion function, but I am learning Typescript and want to know if there are keywords or typescript specific solutions that are meant to be used in such cases, like as Record<string,string>... that can cast it, instead of type assertion

deleted 24 characters in body
Source Link
Abraham
  • 16.2k
  • 12
  • 91
  • 124
Loading
Sorry for the big change, I changed variables to string keyed objects
Source Link
Abraham
  • 16.2k
  • 12
  • 91
  • 124
Loading
added 18 characters in body
Source Link
Abraham
  • 16.2k
  • 12
  • 91
  • 124
Loading
Source Link
Abraham
  • 16.2k
  • 12
  • 91
  • 124
Loading