Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why typescript string & number show never?

Tags:

typescript

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?

like image 236
clencat Avatar asked Dec 21 '21 07:12

clencat


People also ask

Should I use string or string TypeScript?

TypeScript: String vs string 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.

Can you use string in TypeScript?

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.

Why do we use string in JavaScript?

JavaScript strings are for storing and manipulating text. A JavaScript string is zero or more characters written inside quotes.

What is type string in TypeScript?

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.

What is a string in typescript?

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.

Why should I use typescript?

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.

What are primitive data types in typescript?

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

What are the types of JavaScript and typescript?

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.


Video Answer


1 Answers

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>'';
like image 146
Goutham J.M Avatar answered Oct 28 '22 14:10

Goutham J.M