javascript - How to assign either a string or null to a variable TypeScript - Stack Overflow

I have a variable which either can take string or null, currently i am defining all the type values har

I have a variable which either can take string or null, currently i am defining all the type values hardcoded as below

  let name : 'Ram' | 'Shyam' | 'Paul' | null = null;
  name = customer.name as 'Ram' | 'Shyam' | 'Paul' | null;

Here, the values are hardcoded, how can i make it generic so that it can intake anything old and new both.

I have a variable which either can take string or null, currently i am defining all the type values hardcoded as below

  let name : 'Ram' | 'Shyam' | 'Paul' | null = null;
  name = customer.name as 'Ram' | 'Shyam' | 'Paul' | null;

Here, the values are hardcoded, how can i make it generic so that it can intake anything old and new both.

Share Improve this question asked Aug 25, 2020 at 6:04 LaraLara 3,0319 gold badges44 silver badges81 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can check this one

type MyValue = "A" | "B" | "C"
type Nullable<T>  = T | null

const a: Nullable<MyValue> = null

Just declare a custom type

type names = 'Ram' | 'Shyam' | 'Paul' | null;

let name: names = null;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743742298a4499367.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信