<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>TypeScript on Anshik Singh</title><link>https://anshik.in/blogs/typescript/</link><description>Recent content in TypeScript on Anshik Singh</description><generator>Hugo</generator><language>en-us</language><atom:link href="https://anshik.in/blogs/typescript/index.xml" rel="self" type="application/rss+xml"/><item><title>Quick Notes</title><link>https://anshik.in/blogs/typescript/quicknotes/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/quicknotes/</guid><description>🔢 Number # Number is the set of all numbers: integers, floats, positives, negatives, Infinity, NaN, and so on. Numbers can do, well, numbery things, like addition (+), subtraction (-), modulo (%), and comparison (&amp;lt;).
🧩 Primitives in JS/TS # In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types:
string • number • bigint • boolean • undefined • symbol • null</description></item><item><title>Experiments</title><link>https://anshik.in/blogs/typescript/experiments/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/experiments/</guid><description>Assigning object as key in object # let i = { 10: &amp;#34;akkkdkd&amp;#34;, }; const p = { [i]: 888, 10:44, }; console.log(p[&amp;#34;[object Object]&amp;#34;]); // output is 888 wtf console.log(p[10]); copy Ts Unions and Intersections. # type k = { name: string; desc: string; }; type k1 = { name: number; desc1: string; }; type p = k | k1; //union let mmmm: p = { name: 33, desc: &amp;#34;&amp;#34;, desc1: &amp;#34;&amp;#34;, }; mmmm.</description></item><item><title>Arrays in ts</title><link>https://anshik.in/blogs/typescript/arrays/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/arrays/</guid><description>let a = [1, 2, 3]; // number[] var b = [&amp;#39;a&amp;#39;, &amp;#39;b&amp;#39;]; // string[] let c: string[] = [&amp;#39;a&amp;#39;]; // string[] let d = [1, &amp;#39;a&amp;#39;]; // (string | number)[] const e = [2, &amp;#39;b&amp;#39;]; // (string | number)[] let f = [&amp;#39;red&amp;#39;]; // string[] f.push(&amp;#39;blue&amp;#39;); // OK f.push(true); // ❌ Error: Argument of type &amp;#39;true&amp;#39; is not assignable to parameter of type &amp;#39;string&amp;#39; let g = []; // any[] g.</description></item><item><title>Empty Object Literal</title><link>https://anshik.in/blogs/typescript/emptyobjectliteral/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/emptyobjectliteral/</guid><description> // empty object literal (object litral =&amp;gt; syntax to write an object {...}. ) if ({}) { console.log(&amp;#34;Anshik&amp;#34;); } else { console.log(&amp;#34;dkkdk&amp;#34;); } // output is anshik copy</description></item><item><title>Enums</title><link>https://anshik.in/blogs/typescript/enums/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/enums/</guid><description>There are two kinds of enums: enums that map from strings to strings, and enums that map from strings to numbers. They look like this:
enum Language { English = 0, Spanish = 1 enum Language { Russian = 2 } } enum Language { English, Spanish, Russian } // ts automatically merge types copy enum Language { English = 100, Spanish = 200 + 300, Russian // TypeScript infers 501 (the next number after 500) } copy Language[6] // ts won&amp;rsquo;t give error use enum with const to disable unsafe access like Language[6]</description></item><item><title>Extend of types and interfaces</title><link>https://anshik.in/blogs/typescript/extend/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/extend/</guid><description>type are extended using union operators and interfaces are extended using extend keyword
interface # const m = { name: &amp;#34;nashi&amp;#34;, }; interface k { name: string; } interface p extends k { description: string; } const mmm: p = { name: &amp;#34;anhsik&amp;#34;, description: &amp;#34;dkdkk&amp;#34;, }; const ggg: k = mmm; // ggg.description // undefined // inteface follows same java concepts like inheritance, extension and interfaces copy types # // Base type type K = { name: string; }; // Extended type (like subclassing) type P = K &amp;amp; { description: string; }; // Object of extended type const mmm: P = { name: &amp;#34;anhsik&amp;#34;, description: &amp;#34;dkdkk&amp;#34;, }; // Assigning subtype to base type — works because of structural typing const ggg: K = mmm; // ggg.</description></item><item><title>Function Bing Call Apply</title><link>https://anshik.in/blogs/typescript/functions_bind_call_apply/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/functions_bind_call_apply/</guid><description>Overview # call(), apply(), and bind() are JavaScript methods that allow you to control the this context of functions. They&amp;rsquo;re essential for function borrowing, method chaining, and controlling execution context.
The &amp;lt;code&amp;gt;call()&amp;lt;/code&amp;gt; Method # Syntax # function.call(thisArg, arg1, arg2, ...) copy Basic Example # const person = { firstName: &amp;#34;John&amp;#34;, lastName: &amp;#34;Doe&amp;#34;, fullName: function () { return this.firstName + &amp;#34; &amp;#34; + this.lastName; }, }; const anotherPerson = { firstName: &amp;#34;Jane&amp;#34;, lastName: &amp;#34;Smith&amp;#34;, }; // Using call to borrow the fullName method console.</description></item><item><title>Null, Undefined, Void and Never</title><link>https://anshik.in/blogs/typescript/null-undefined-void-never/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/null-undefined-void-never/</guid><description>Null and Undefined # undefined type means the type has not been yet defined. And null means absence of a value it is defined but absent it is defined but can we access or not we don&amp;rsquo;t know.
Void and Never # Functions that never return (throw, infinite loops) are inferred as returning never by good ts programmers. void means a function will execute completly. for its scope.
unkown and never # // unknown is the supertype of all types let value: unknown; value = 42; // OK value = &amp;#34;hello&amp;#34;; // OK value = true; // OK value = { x: 1 }; // OK // a variable of type unkown can accept any type of value as given copy // at the same time never is the subtype of all types → Bottom type let neverValue: never; // You can assign `never` to any type: let str: string = neverValue; let num: number = neverValue; // err str is assign berfore initialization but its not giving type error // OR function k(): never { throw TypeError(&amp;#34;eee&amp;#34;); } let m: string = k(); // a variable of type never can be assigned to any type or it is accepted by any type in ts.</description></item><item><title>Overloaded Function Types</title><link>https://anshik.in/blogs/typescript/overloaded_function_types/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/overloaded_function_types/</guid><description>Signature call is syntax in ts to prodifine prototype/type of function. There are two type to write a signature call.
First is Short hand signature call. # type Log = (message: string, userId?: string) =&amp;gt; void; copy as simple as that
Second is Full Call Signature # type Log = { (message: string, userId?: string): void; }; copy Now, Full Call Signature comes with very very powerful feature of TypeScript: the ability to overload function types</description></item><item><title>Strings</title><link>https://anshik.in/blogs/typescript/string/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/string/</guid><description>Strings in js/ts # When String() is called as a function (without new), it returns value coerced to a string primitive. one
But When String() is called as a constructor (with new), it coerces value to a string primitive (without special symbol handling) and returns a wrapping String object, which is not a primitive. one
For Example:
const a = new String(&amp;#34;Hello world&amp;#34;); // a === &amp;#34;Hello world&amp;#34; is false const b = String(&amp;#34;Hello world&amp;#34;); // b === &amp;#34;Hello world&amp;#34; is true a instanceof String; // is true b instanceof String; // is false typeof a; // &amp;#34;object&amp;#34; typeof b; // &amp;#34;string&amp;#34; copy String behaviour with Symbols # const sym = Symbol(&amp;#34;example&amp;#34;); `${sym}`; // TypeError: Cannot convert a Symbol value to a string &amp;#34;&amp;#34; + sym; // TypeError: Cannot convert a Symbol value to a string &amp;#34;&amp;#34;.</description></item><item><title>When and where to use unkown?</title><link>https://anshik.in/blogs/typescript/unkown/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://anshik.in/blogs/typescript/unkown/</guid><description>intro # For the few cases where you have a value whose type you really don’t know ahead of time, don’t use any, and instead reach for unknown. Like any, it represents any value, but TypeScript won’t let you use an unknown type until you refine it by checking what it is
Example # let a: unknown = 30 // unknown let b = a === 123 // boolean let c = a + 10 // Error TS2571: Object is of type &amp;#39;unknown&amp;#39;.</description></item></channel></rss>