I was studing Typescript, but I have some confusion.
type t = number & string // never
type t1 = number & boolean // never
type t2 = number & string[] // number & string[]
Why does it look different?
TypeScript: String vs string 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
Just like JavaScript, TypeScript also uses double quotes ( " ) or single quotes ( ' ) to surround string data. You can also use template strings, which can span multiple lines and have embedded expressions.
JavaScript strings are for storing and manipulating text. A JavaScript string is zero or more characters written inside quotes.
String is another primitive data type that is used to store text data. String values are surrounded by single quotation marks or double quotation marks.
In TypeScript, the string is sequence of char values and also considered as an object. It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string. TypeScript string work with the series of character.
There are two main reasons to use TypeScript: TypeScript adds a type system to help you avoid many problems with dynamic types in JavaScript. TypeScript implements the future features of JavaScript a.k.a ES Next so that you can use them today. This tutorial focuses on the first reason.
TypeScript - String String is another primitive data type that is used to store text data. String values are surrounded by single quotation marks or double quotation marks. Example: TypeScript String Type Variable
We’ll start by reviewing the most basic and common types you might encounter when writing JavaScript or TypeScript code. These will later form the core building blocks of more complex types. JavaScript has three very commonly used primitives: string, number, and boolean . Each has a corresponding type in TypeScript.
In Typescipt the instersection &
of two primitive types is always never because a variabe cannot be both string and a number, but the intersection of two array/object is called as branded object
and one array/object with a primitive is a valid type and is called as branded primitve
, so where do we use branded primitive ?
,
refer the below example
type SomeUrl = string & {'this is a url': {}};
let x = <SomeUrl>'';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With