Typescript - Basics (Sections 1-4)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

In the tsconfig.json file, "noUnusedParameters" is unseen and therefore set to false by default. What does setting it to true do?

Throws a compilation error if you have any functions/methods that take in parameters but never use them.

True or False: In Typescript, functions are types on their own.

True

True or False: The spread and rest operators have the exact same notation.

True

True or False: Typescript does NOT care about integers vs. floats, they're just all type number.

True

True or False: When Typescript gets compiled to Javascript, all the types go away.

True

True or False: You can chain more than two types when using Union types.

True

True or False: You can set default values for function parameters.

True

True or False: You can use the "var" keyword in ES6 to create a variable.

True

let myName = 'Nick'; myName = 32; True or False: Even though you didn't explicitly declare myName to be of type string, Typescript infers that it's a string based on its value and will throw an error on compilation.

True

True or False: You can reference a previously established parameter in the default value for another parameter.

True (The key words being "previously established.")

True or False: If you have a function that just, for example, throws a new error (instead of returning anything), you should NOT give it the 'void' return type.

True Instead, you use Typescript 2.0's new type -- never.

In the tsconfig.json file, "sourceMap" is set to false by default. Setting it to true gives you one main benefit -- allowing you to ______________ your Typescript files in the ______________.

debug browser (And also set breakpoints)

The spread operator looks like an _________________ and basically turns an ________________ into a _________________.

ellipses array list

A good rule of thumb with using any as a type is it should be an ______________________ for you to use it.

exception

Template literals are just strings with more ___________.

features

They're called "arrow functions" because you use an arrow ( => ) to point to the _____________ __________.

function body

You can think of the rest operator as adding the __________ _______________ on a list to make it an array.

square brackets

You can think of the spread operator as removing the __________ _______________ on an array.

square brackets

If a function doesn't have a return statement (it doesn't 'return' anything), what type should be used?

void function sayHello(): void { console.log('hello'); }

Good practice is to use the "const" keyword instead of "let" whenever you know what?

The value is not going to change.

ES6 is _______-_______________ Javascript. (At least, as of 2017.)

next-generation

Template literals are an ES6 feature that gets rid of the need for ________ operators for more complicated strings.

plus (+)

If you use '...' when defining a parameter for a function, ES6 knows it's the _________ operator. If you use '...' in a function call (passing something to a function), ES6 knows it's the __________ operator.

rest (turn a list into an array) spread (spread out the array into a list)

The difference between "let" and "var" is the _________ of the variable.

scope

True or False: If you specify the type returned from a function as "void," you can still return something from that function without throwing an error.

False

True or False: Typescript runs in the browser.

False (It must be compiled down to JavaScript.)

After compilation, both "let" and "const" become what?

"var"

Typescript is a "______________" around JavaScript.

"wrapper"

How do you output a variable myVariable inside a template literal?

${myVariable}

What is the difference between 'void' and 'never' as a return type for a function?

'void' returns nothing 'never' never returns. (For example, it throws an error, so therefore never returns.)

What old ES5 function do the rest and spread operators remove the need for?

.apply()

What are the three main benefits of template literals compared to the old way of going about more complicated strings?

1. Multi-line strings 2. Output of variables without "exiting out" of the string. 3. No need for single-quote/double-quote debate. const greeting = `This is a heading! My name is ${myName}. Yo! The "end" is 'near'. `;

If you want to be super-strict about not allowing null variables throughout your code, what two things do you have to do?

1. Open tsconfig.json 2. Add "strictNullChecks": true to the "compilerOptions" object.

What two things does a tuple do to help with Typescript?

1. Places a hard limit on the number of items that can appear in an array. 2. Defines what type each of those positions in the array should be. let myArray: [string, number, string]; means exactly three items should appear in myArray, and each position should be what is defined.

Typescript code will run in all browsers because it compiles to ____________ even though you write it using ___________ features.

ES5 ES6

When defining what type should be returned from a function, where does it go?

After the function name and parentheses. function getName(): string { // code here }

What does "block-scope" mean in regards to methods/functions?

All variables declared inside a method/function are limited to that scope.

Template literals are created using what unique characters?

Backticks ``

If you have an array of strings, what happens if you try to append (or replace) a number to that array?

Compilation error

What happens in Typescript if you try to change the value of a const?

Compilation error

let hasHobbies = false; hasHobbies = 1; True or False: Typescript will NOT throw an error here because 1 can be cast to true.

False (It will throw an error and treat 1 as a number. Typescript likes numbers to be numbers and booleans -- true/false -- to be booleans.)

True or False: Typescript supports all features of ES6.

False (To get details on what is and is not supported, go to http://kangax.github.io/compat-table/es6/)

True or False: Setting "noImplicitAny" to true in the tsconfig.json file makes it so you can't have variables with type 'any'.

False (You can, but you just have to be explicit in declaring them as 'any'.)

True or False: You canNOT set a function as a type for a variable.

False (You can.)

True or False: Arrow functions are a feature specific to Typescript.

False It's an ES6 feature.

True or False: If you know a variable will be one of two types (number or string), but never anything else (like a boolean), you have to give it a type of "any."

False Use Union types to allow multiple types! let myAge: number | string = 27; myAge = "27";

The spread operator is a new ES6 feature that allows you to do what?

Flexibly transform arrays without having to write any complicated logic like loops.

What can you add to the tsconfig.json file if you don't want Typescript to compile when there's errors?

In the "compilerOptions" object, add "noEmitOnError": true

Where are Template Literals used within Angular4?

Inline templates

What special character is used for Union types to allow multiple types?

The pipe | let myVariable: string | boolean = "false";

"let" creates a block-scope variable. What does that mean?

It's contained in the block in which you define it.

What is the language used in the browser to make websites reactive?

JavaScript

In the tsconfig.json file, "noEmitOnError" isn't in there by default (so it's false). Adding it and making it true will make it so that no ____________ file is created if there's an error in the ________________ file.

Javascript Typescript

What does adding "strictNullChecks": true to the "compilerOptions" object in tsconfig.json essentially do?

Not allow a variable to be null after its type has been inferred or declared to be something like string, number, etc.

What is the opposite of ES6's spread operator?

Rest operator

What are the three most basic types in Typescript?

Strings, Numbers, Booleans

What are three examples of more advanced ES6 features supported by Typescript, but they don't exactly have anything to do with Typescript?

Symbols Iterators Generators

Without creating a class, how can you tell Typescript to create a new, custom type?

The "type" keyword type Complex = {data: number[], property: string};

What main thing does the new arrow function syntax take away?

The 'function' keyword.

What is the most important feature of Typescript? (Hint: It's in the name!)

The fact that it's strongly typed.

When using ES6 to destructure an object into individual variables, what is the most important thing to remember?

The names for the variables must match up with the keys in the object. const scientist = {firstName: "Will", experience: 12}; let {firstName, experience} = scientist; You can name the variable something else if you want, but the original key still has to match up. const scientist = {firstName: "Will", experience: 12}; let {firstName: poop, experience} = scientist; console.log(poop, experience);

What is the most important thing to remember when using the "typeof" keyword to check for types during runtime?

The types MUST go in quotes, otherwise it will not work. if (typeof myAge == "number") { // do some code here }

If you have variable that will be a mixed array, but you know that it will ALWAYS include a string in the first position and a number in the second, should you use a type of any[]? What should you use instead?

Use a tuple. let myArray: [string, number] = ['Super Street', 28];

The rule in Typescript/Javascript is on the right side of the = sign, you set a value. What is the exception in Typescript?

Using the "type" keyword. Even though it's on the right side of the = sign, that's still defining its type, not its value. type Complex = {data: number[], property: string};

What is the main issue with type 'any'?

We don't get any of the benefits Typescript offers us. It basically just makes it normal JavaScript.

With arrow functions, in what situation do you NOT have to explicitly write 'return' for what you're returning?

When the function body is only one line. multiply(num1: number, num2: number) => num1 * num2

With arrow functions, what is the only situation in which you do NOT need to put parentheses around the function parameters?

Where there is only one. For zero, or two or more, you need parentheses.

If Typescript throws a compilation error, will it keep compiling by default?

Yes

According to Typescript, is NaN of type number?

Yes. (It's weird, because "Not A Number" is in fact a number. It's Typescript saying, "Yeah, we were expecting a number, but it isn't.")

What is the "gotcha" when leaving out parentheses when there's only one parameter for an arrow function?

You can't declare the parameter's type. const greetFriend = friend: string => console.log(friend); That's invalid with type string! It either needs parentheses or the string must be removed.

In the tsconfig.json file, "noImplicitAny" is set to false by default. Setting it to true changes what?

You can't establish a variable like this... let anything; Which implicitly gives anything the 'any' type. Now, that code will throw a compilation error.

If you want to assign a variable to be a (previously created) function, do you use parentheses? Why or why not?

You do not. You don't want to set the variable equal to the return value of the function, but instead to the function itself.

What does it mean to be a "strongly typed" language?

You have to be specific which type a variable is.

What advantage does initializing your project for Typescript (with "tsc --init") give you?

You no longer have to specify which files you want to compile with the "tsc" command. By simply running "tsc", it will search through all .ts files and compile them.

Using any as your type gives up all the __________________ that Typescript offers.

advantages

If you don't declare a type nor give it an initial value, Typescript will infer that its type is _________.

any

If you establish a variable with... let myAge; But don't give it an initial value, Typescript treats this as what type?

any

What is the most flexible type in Typescript?

any

How could you allow an array to accept any types to it?

any[] let myArray: any[] = ['string', 100, false];

Tuples are basically just ___________ with mixed ___________.

arrays types

"let" creates a ___________-__________ variable. "var" creates a __________-__________ variable.

block-scope global-scope

How does Typescript relate to JavaScript? (It ___________ __________ to JavaScript.)

compiles down

You have a variable named 'hobbies'. How can you print the type of that variable to the console?

console.log(typeof hobbies);

You can include other parameters in the same function as a rest parameters, but the rest parameter must be ___________ in the argument list.

last

The ES6 syntax to create variables is ________ instead of _________.

let var

You want to define a variable 'myFunction' that... A) Is a function B) Can only take in two numbers as arguments. C) Will return a number. How would you set this up to give it that type?

let myFunction: (val1: number, val2: number) => number;

The spread operator is appropriately named because it "spreads" out an array into a _______________ of single __________.

list, values [10, 20, 3, 400] becomes 10, 20, 3, 400

When using 'never' as the return type for a function, you should think, "Oh, if I go there, I will ________ _________."

never return

What command is available in the terminal after you install Typescript globally with npm?

tsc

What command do you run to tell a directory (or project) to be under control of Typescript? What file does it create?

tsc --init tsconfig.json

How do you let Typescripe "watch" your files so you don't have to keep compiling with the "tsc" command after every change?

tsc -w

How do you use the tsc command in the terminal? What does it do?

tsc script.ts Compiles Typescript by making a .js file of the same name (script.js)

In what file can you change how the Typescript compiler behaves?

tsconfig.json

Typescript adds a data type that Javascript does not allow. It's called a _________, which allows you to define what data can appear in each position in an array.

tuple

What are two new types added with Typescript?

tuples and enums (See Section 2, Lecture 14 for more on enums because I'm not sure how relevant they are.)

What keyword can you use to tell Typescript, "create a new type"?

type

What keyword can you use to check for types during runtime?

typeof

If you declare a variable but don't give it an initial value, what is its default value in Typescript/Javascript? let myVar;

undefined

ES5, ES6 (and eventually ES7) are _____________ of JavaScript.

versions


Ensembles d'études connexes

Chapter 22 (Management of Patients with Upper Respiratory Tract Disorders)

View Set

Bio Chapter 17 - Olfaction & Gustation

View Set

Chapter 10: Baroque Instrumental Music

View Set

Chemistry- Chem 101 Homework (Exam #2)

View Set

Finding the Mode of a Data Set, 4

View Set

Computer Networks (Ross and Kurose)

View Set

World History: New Economic Theories

View Set