The Basics Flashcards
What is type narrowing?
Moving from a less precise type to a more precise type.
What is the type of button here:const button = document.querySelector(".go");
Element | null
Will a type error occur here if so why?
const button = document.querySelector(".go"); if (button) { button.disabled = true; }
Yes. Here the type of button will be Element | null and these don’t have a disabled property.
How can we narrow the type of button here using <> to HTMLButtonElement:const button = document.querySelector(".go");
const button = <HTMLButtonElement>document.querySelector(".go");
How can we narrow the type of button here using the as syntax to HTMLButtonElement:const button = document.querySelector(".go");
const button = document.querySelector(".go") as HTMLButtonElement;
Which syntax is preferred for type narrowing? The <type> or as syntax?</type>
The as syntax is preferred
Will Typescript be able to infer that text is not null on the return line here?
function duplicate(text: string | null) { let fixString = function() { if (text === null || text === undefined) { text = ""; } }; fixString(); return text.concat(text); }
No. In this case Ts can’t infer the type. The easier solution is to use the non-null assertion operator !
function duplicate(text: string | null) { let fixString = function() { if (text === null || text === undefined) { text = ""; } }; fixString(); return text!.concat(text!); }
What is the Non-null assertion operator syntax?
The non-null assertion operator is an exclamation mark (!), and this is placed after the variable or expression that we want to tell TypeScript isn’t null or undefined. We should only use this when we definitely know the variable or expression can’t be null or undefined.
What is the type of item in the if branch? And in the else branch?
function double(item: string | number) { if (typeof item === "string") { return item.concat(item); } else { return item + item; } }
string in the if branch and number in the else branch.
What will be the type detected by your IDE here? And what will be the output to the console?
const wtv = "myname"; console.log(typeof wtv);
Here the type is the string litteral “myname” but the output will be string. Why? Because typeof is a Javascript runtime operator and doesn’t about Typescript type at runtime (they are stripped).
What is typeof and when is it useful?
It is useful for type guard and is called a typeof type guard. It’s useful on variables or expressions with primitive types.
What is the instanceof operator?
instanceof is a JavaScript operator that can check whether an object belongs to a particular class. It also takes inheritance into account.
Using instanceof How would you add the missing todo knowing that Person and Organisation extend Contact?
class Contact { constructor(public emailAddress: string) {} } class Person extends Contact { constructor( public firstName: string, public surname: string, emailAddress: string ) { super(emailAddress); } } class Organisation extends Contact { constructor(public name: string, emailAddress: string) { super(emailAddress); } } function sayHello(contact: Contact) { // TODO - Output Hello {firstName} if a person // TODO - Output Hello {name} if an organisation }
By using the instanceof operator:
function sayHello(contact: Contact) { if(contact instanceof Person){ console.log(contact.firstName); }else if(contact instanceof Organisation){ console.log(contact.name); } }
What is the in operator? What is the syntax used?
in is a JavaScript operator that can be used to check whether a property belongs to a particular object.
The syntax is:
propertyName in objectVariable;
How would you solve this using the in operator? Knowing firstName is a field of defined on the Person interface and name is a field defined on a Organisation interface and Contact is defined:
interface Person { firstName: string; surname: string; } interface Organisation { name: string; } type Contact = Person | Organisation; function sayHello(contact: Contact) { // TODO - Output Hello {firstName} if a person // TODO - Output Hello {name} if an organisation }
function sayHello(contact: Contact) { if ("firstName" in contact) { console.log("Hello " + contact.firstName); } if ("name" in contact) { console.log("Hello " + contact.name) } }
Note this is error prone. If there is a typo in the name Typescript won’t always raise an error and you can end up with a Record or something else in the branch.