TypeScript

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Difference between for-in loops and for-of loop

for in: Iterate over the key in an array If using for-in for array or string, it will iterate over the index. for of: Iterate over the element in an array of char of the string. Compile error if used with an object that isn't an array or string.

Describe the kinds of TypeScript feature that may be included in Hyperspace Web Code

1. Approved 2. Not Approved 3. Not Evaluated We should only use any language features that are approved. If new feature is not in any three of categories, assume its not evaluated

What happen when Typescript is compiled

1. Check syntax and type issue. 2. JavaScript code that meets the select ECMAScipt standard is emitted, alsong with a .js.map file that allows script debugger to present the original TypeScript code when debugging. Notes:we use transpiled rather than compiled. ECMAScript version depends on browser version. Chrome use ECMA6 and IE use ECMA5, then target version must be 5.

Why use Typescript for new Hyperspace Web client Code

1. Typescript is less likely to have bugs resulting from mismatched type 2. VisualStudio support is better for Typescript than JS, such as code refracting and IntelliSense 3. More language feature than JS, making the code easier to read conclusion: fewer bugs and increased programmer productivity and job satisfaction.

How Javascript code generated from Typescript ids debugged in client browser

Even though browser is executing Javascript. Debugging will take places in Typescript when using browser's developer tool. This feature is available because the browser was provided with the original .ts file as well as .js.map file that enables it to map the currently executing line of JavaScript to the original line of TypeScript.

Describe how you could make a function more flexible in Typescript?

1. Use optional and default parameters - use ? between name and data type to mark parameter as optional. - optional parameters must come after required parameter. - Developer can also choose to supply a default value for parameter 2. Allowing an arbitrary number of parameters - Must be same type function Name(...args:string[]){ } 3. Function overloading - Programmer can define multiple signatures so caller can call this function in multiple way. - only one implementation for all signatures. 4. Creating named parameter - If function has large amount of optional parameters. It's convenient for the consumer to create named parameters so caller only pass in what actually need. function createButton(caption: string, args?: {autoHide?: boolean, isDefault?: boolean, isCancel?: boolean}) { createButton("OK", {autoHide: true, isCancel: true});

Describe the syntax of declaring type alias and when you would do so.

Allows you to declare an identifier that you can use as a custom data type. The type alias can combine primitive data types using union-type syntax, or it can declare an object shape, specifying properties that an object must possess. In this way type allias are similar to interfaces. - Epic convention is to use interface. - The one case where interface will not work is when aliasing a union type. In this case, use type alias. Only use type alias if you're encapsulating a union type.

Describe the syntax of how a method is defined within a class

Behavior is added to a class by defining its methods. In Typescript, methods are declared like fields, but also include a parameter list and a code body, just like function definition. void function does not need return.

How you would declare a function/method type?

C# can do this using delegate keyword, not exist in Typescript. We should use the interface keyword: Interface FunctionTypeName { (param1: type1, param2: type2,....): Return type; }

Explain how to overload a function in TypeScript?

Can have multiple signature but only one implementation function example(): boolean; function example(str: string): string; function example(bool: boolean): boolean; function example(arg?: string | boolean): string | boolean{ . . . }

Difference between illegal type assertion and incorrect type assertion.

Illegal: compiler error, if the cast cannot be performed given the data types of the source and target value. EX: a number can't cast to string Incorrect: run time error. ex: asserting "any" variable is a legal, but if the value is not string at runtime, then TypeError will be throw.

How scoping a variable with "let" differ from "var"

In Javascript: var scoped within nearest containing function or catch block. If its not contained within either of those then it is a global variabled. TypeScript: "let" scoped within the nearest containing block of any kind. If declared let outside of a block and redeclared var inside of block will cause a compile error, since two variable would share the same scope( redeclare using let inside block will work)

Describe how to declare members of a base class that can be override by a subclass and how the override is accomplished.

In TypeScript, all member can be overriden, so no need for virutal, overriden, new or sealed keyword. - Simply redefined the member in derived class no keyword needed. 1. new implementation must have same or more relaxed access level than the original 2. new implementation must have same signature and return type You can access base class implementation by using "super" keyword.

Describe the syntax of declaring a constant and the limitations on variables declared in this way

Similar to C#, declare with "const" keyword. The value must be declared when the variable is declared( value can be computed at the run time) Once declared, the value cannot change. If the value of the constant is a reference type, like an Object or a function, then its properties can still be changed.

Describe the syntax for declaring an object type, including optional properties and index members.

It's possible to declare a variable that must conform to a specified object shape, including optional properties. - Only object matching that shape can assign to that variable - You may also declare index member, specifying its name, index type and value type( index type and value type must allow for all other existing properties of the object)

Explain why you would encapsulate an index type in an interface.

Make code more readable and reusable. You can then program against the interface. It constrains the type of object that an array can contain. This provides a level of type safety to your code.

Identify the access level of a member of a class that is not given explicit access modifier

No explicit access modifier in TypeScript will default to public All class member write in Epic should given a explicit access modifier. (In C#, its private)

Explain why you would create an interface with optional members

Optional interface members make it possible to create interfaces that allow the consumer to "fill- in - the - blanks". It allows for the creation of objects against the interface even though the object itself does not adhere completely to the type

Describe the appropriate syntax for declaring properties of a class?

Rules: The access modifier of getter and setter must be same The return type of the getter must match the type of setter parameter. Either the getter ( write-only) or setter ( read-only) can be omitted. Epic Convention: write only property are not allowed because they are not well supported by TypeScript when combined with interfaces. Create a regular method named with a prefix of "set" instead:

Describe the difference between interfaces in TypeScript and C#

Similar: - Can declare method and property - All member are public - All member are abstract - Classes that implement an interface must implement all member that are not marked as optional - One interface can extend another - A class may implement multiple interfaces. Differences: - Property declarations cannot specify getter or setter. - No explicit implementation in TS - Can mark member as optional using ? - Can encapsulate array types: interface StringArray{[index: number]: string;} Only string and number are supported index types. - If a property of a class has a getter without a setter and is type-asserted to an interface that requires a field of the same name as the property, then compiler will not complain if you attempt an assignment on that property. denpong on browser, you may or nay not see an error at runtime, but it will fail. - Only the "shape" of the interface matters to determine adherence to the type. Any object works even if it does not implement the interface, provided that all required member of the interface are provided. This is know as "duck" typing. If it looks like a duck and quack like a duck, then it must be a duck.

Describe the difference between Generic in TypeScript and C#

Similar: Similar syntax can be applied to classes, interfaces, and methods/functions. can apply constraints to supplied types. Difference: Duck typing of interface can be used with generic constraints.

Similarities and Difference between comment format in TypeScript and JavaScript?

Similarities: 1. Support single line commnet //comment 2. Support block comment /* comment */ ----------------------------------------------------- Differences: API documentation TypeScript use JSDoc -style /** * Description of the method or function * @param paramName This is the parameter description */ JS use XML- style ///<summary>Description of the method or function</summary> ///<param name="paramName">This is the

Describe the naming convention and access modifier that should be used when declaring a member of a class that should only be accessible from within the project is defined. also explain how such a member should be accessed by other classes from the same project.

TypeScript does not have internal keyword. We should use public keyword instead, but programmer intention to make it internal is indicated by name it using _camelCase and add comment /** @internal */ directly above it. This comment will strip the member from the type definition file (*.d.ts) that is referenced by other projects, so it is no longer visible.

Describe the data types in TypeScript and how they compared to their C# counterparts

TypeScript is the superset of JavaScript, all type from Javascript is supported in Typescript. Typescript support: Boolean, String, Number and following new type: any - can be any type, which disables type checking ( similar to using the object in C#) enum - represent one of a predefined set of named numeric value. void - used with function

Explain how you can create difference constructor with the same class

Typescirpt only allow you explicit constructor function implementation per class. You can provide multiple overloads with difference signature but they can only have one implementation. The name of each constructor signature function will be constructor, rather than the name of the class. If a constructor function is not explicitly written, a default public constructor will be implicitly added to the generated JavaScript. class MyClass { public constructor(inputParameters1) //1st Constructor declaration public constructor(inputParameters2) //2nd Constructor declaration // Remember: The implementation function is actually called "constructor" public constructor(inputParameters) //Constructor implementation must account for all possible declared signatures { // Initialize object based on input } }

Describe the difference for declaring an enumerated type and how they compared to C# counterparts

Typescript and C# both declared with the "enum" keyword( begining at 0 and increment by 1) Typescript: - non-interger value can be assigned to be enum members. - subsequent members continue to increment by 1 (e.g., 1.1, 2.1). -indexed, member name can accessed by using numberic value: enumName[numericValue]; Can be accessed by: enumName.memerberName enumName['memberName'] const enum: Does not allow to access member name by using numeric values. More efficient because numeric value will directly replace calls to enumName.memberName and enumName['memberName'] when generate JS code.

Explain how members of the base class, such as base constructor or an override method, can accessed from a sub class

Unlike C#, TypeScript access the base class using the super keyword rather than the base keyword. subclass constructor must always include a call to super. ( even if base class has a parameter-less constructor defined, regardless of whether or not default constructor is explicitly or implicitly

Describe the appropriate syntax to declare that one class inherits from another class

Unlike C#, TypeScript establishes inheritance using the extends keyword: class BaseClass{ } class DerivedClass extends BaseClass

What a type guard is and when you would use one.

Use typeof and instanceof keyword within an if statement. This provides a simple method of avoiding incorrect type assertions. Use typeof with number or string Use instanceof with object such as array

How you can provide get and set accessors to a property with different access modifiers

get and set for same property must have the same access modifier. The best way to get around this is to create read-only property with a separate set method. private __propertyBackingField: Type; AccessModifier get propertyName(): Type {}; DifferentAccessModifier setPropertyName(value: Type): void

Explain how a type guard behaves when it can definitively determine a union variable's type vs how it behaves when it can't

if we have if-else branches that depend on a union types variable. If variable is sent to constant, then type guard can determine the code path at compile time and alert you to code if that may be unreachable. If variable get its value from something other than a constant( return value of a function), the type guard can't tell what type the variable will be. In this case, it will narrow the variable's type and only allow access to members that match that type.

Describe the syntax for union types and when and how you would use them

to declare a union type, you can use pipe character "|" between types. This is more safety than use any type, but restrict the available methods to only those that any of the union type can access.

Syntax difference for declaring explictly and implicitly typed variabls, parameters, and function return type.

variables, function parameter and function return type can all declare declared with data types. If no data type is declared when variabled is declared, then its become any. If the value is assigned to typeless variable, parameter or function as it being declared, then variable's type will be inferred from the value assigned. Epic convention is to explicitly declare the data types of variables and parameters unless it is immediately obvious what the inferred type is. Return type of all function should be explictly declared.


Set pelajaran terkait

Finance and Marketing(Related)- chapter 17

View Set

US Military Phonetic Alphabet Tables

View Set

Chapter 2: Collecting Subjective Data: The Interview and Health History PrepU Quiz and answers

View Set

NUR 1050 Fundamentals II Ch 2 Theory reasearch and EBP

View Set

anatomy and physiology exam#2 chapter 5

View Set