tf-typescript
Sure, here are 10 True/False questions related to TypeScript:
TypeScript is a superset of JavaScript that compiles to plain JavaScript. (True/False)
In TypeScript, all parameters are optional by default and do not require an explicit
?
to denote an optional parameter. (True/False)TypeScript's type inference allows you to get a lot of power without writing additional code. (True/False)
TypeScript can only be run in environments that support ECMAScript 6. (True/False)
Enums in TypeScript can be either numeric or string-based. (True/False)
True. TypeScript is a superset of JavaScript that compiles down to plain JavaScript.
False. In TypeScript, parameters are not optional by default. You must explicitly mark parameters as optional with a
?
if they are not required.True. TypeScript's type inference does allow for powerful typing without the need for additional code.
False. TypeScript can be compiled down to any ECMAScript version (such as ES3 or ES5) depending on the configuration, so it is not limited to environments that only support ECMAScript 6.
True. Enums in TypeScript can indeed be numeric or string-based.
- The
any
type in TypeScript is equivalent to opting out of type checking for a variable. (True/False) - TypeScript has a special type called
unknown
that is more type-safe thanany
. (True/False) - Private methods in a TypeScript class can be accessed from instances of that class. (True/False)
- TypeScript interfaces can be used to describe the shape that values should have. (True/False)
- Generic types in TypeScript can only be used with classes and interfaces, not functions. (True/False)
You can answer these True/False questions, and I'll provide feedback on your responses.
Certainly! Here are the answers to the True/False questions provided:
- True. The
any
type in TypeScript effectively disables type checking for that variable. - True. The
unknown
type is indeed more type-safe thanany
because it forces you to do some form of checking before you operate on values of typeunknown
. - False. Private methods in a TypeScript class cannot be accessed from outside the class or its instances; they are meant to be accessible only within the class itself.
- True. Interfaces in TypeScript are used to type-check the shape that objects have.
- False. Generic types in TypeScript are versatile and can be used with functions, classes, interfaces, and other structures.
These answers can be helpful in checking your understanding of TypeScript's features. If you have more questions or need further clarification, feel free to ask!
Absolutely, here are five True/False questions on TypeScript with their respective answers:
Question: TypeScript offers a
never
type which represents types that never occur.- Answer: True. The
never
type is used for values that never occur, such as a function that always throws an error or a function that never returns.
- Answer: True. The
Question: A class in TypeScript can implement multiple interfaces.
- Answer: True. TypeScript classes can implement multiple interfaces by separating each interface with a comma.
Question: The
--strictNullChecks
flag can be used to catch null or undefined assignment errors at compile time.- Answer: True. When enabled, this flag makes TypeScript raise errors if null or undefined values are assigned to a variable not explicitly marked to accept them.
Question: The TypeScript compiler can automatically infer the return type of a function without explicitly declaring it.
- Answer: True. TypeScript can infer the return type of a function based on the return statements present in the function.
Question: In TypeScript, you can use both
let
andconst
for variables that change during the application's lifecycle.- Answer: False. In TypeScript (and JavaScript),
let
is used for variables that can change, whileconst
is used for variables that should not change (immutable) after they are set.
- Answer: False. In TypeScript (and JavaScript),
These questions and answers should help as you study TypeScript. If you need further explanations or additional questions, feel free to ask!
Certainly! Here are five intermediate to advanced True/False questions about TypeScript, along with their answers:
Question: In TypeScript, the
unknown
type is automatically assigned to variables that are initialized without a value.- Answer: False. The
any
type is automatically assigned to variables that are initialized without a value if the--strict
flag is not enabled. Theunknown
type is not automatically assigned but should be explicitly set by the developer when the type of a variable is not yet known.
- Answer: False. The
Question: Decorators in TypeScript can be applied to a class, method, accessor, property, or parameter.
- Answer: True. Decorators are a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form
@expression
, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.
- Answer: True. Decorators are a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form
Question: TypeScript's
keyof
operator can be used on types to fetch the set of known, public property names as a union type.- Answer: True. The
keyof
operator takes an object type and produces a string or numeric literal union of its keys.
- Answer: True. The
Question: The TypeScript compiler can emit specific ECMAScript standard JavaScript (like ES5, ES6) based on the
target
setting in thetsconfig.json
file.- Answer: True. The
target
setting in thetsconfig.json
file specifies the ECMAScript target version that the compiler will output.
- Answer: True. The
Question: A tuple in TypeScript is a kind of array where the type of a fixed number of elements is known, but not necessarily the same.
- Answer: True. Tuples in TypeScript are an array-like structure where each element represents some property of a record.
I hope these questions aid in your TypeScript study and deepen your understanding of some of its more nuanced features. If you have any more questions or need further information, let me know!
Here are five intermediate to advanced True/False questions on TypeScript, along with the correct answers:
Question: TypeScript's
Partial
utility type makes all properties of a type optional, effectively creating a type with the same properties, but with each one marked as optional.- Answer: True.
Partial<T>
constructs a type with all properties ofT
set to optional.
- Answer: True.
Question: The
as
keyword in TypeScript is only used for type casting when the--strict
flag is enabled in the compiler options.- Answer: False. The
as
keyword is used for type assertions in TypeScript and is not dependent on the--strict
compiler flag.
- Answer: False. The
Question: In TypeScript, an
interface
can extend multipleclasses
.- Answer: True. An interface can extend one or more classes, which can include a mix of both types and values.
Question: The
unknown
type in TypeScript is assignable to any other type without a type assertion.- Answer: False. While any type is assignable to
unknown
, you cannot assignunknown
directly to any other type (except forany
) without a type assertion.
- Answer: False. While any type is assignable to
Question: TypeScript allows for overloading function implementations, but the number of arguments must remain the same across overloads.
- Answer: False. TypeScript function overloads can differ in the number and types of their parameters, not just in the return type.
These questions cover some nuanced aspects of TypeScript's type system and its capabilities. They should help you in preparing for scenarios you might encounter when working with TypeScript at an intermediate or advanced level. If you need explanations or have other questions, feel free to ask!
Certainly! Here are five intermediate/advanced TypeScript True/False questions with their answers for your study:
Question: The
readonly
keyword in TypeScript can be used to make properties of an interface immutable after their initial creation.- Answer: True. The
readonly
modifier ensures that the properties of an interface can't be reassigned after their initial assignment.
- Answer: True. The
Question: TypeScript's discriminated unions can only be formed using string literal types.
- Answer: False. Discriminated unions, also known as tagged unions, can be created using string literals, numbers, or boolean literals as the discriminant.
Question: The
--noImplicitReturns
flag in the TypeScript compiler will report an error if not all code paths in a function return a value.- Answer: True. This flag checks that all functions have a return value on all code paths.
Question: When a function has overloads, TypeScript will always use the implementation signatures to determine the correct type.
- Answer: False. TypeScript uses the list of overload signatures to resolve the types, not the actual implementation signature which is not visible to the caller.
Question: TypeScript's
namespace
keyword is deprecated and modules should be used instead.- Answer: False. While modules (ES6 modules) are generally preferred for their better scoping and reusability, the
namespace
keyword is not deprecated and may still be used, particularly for organizing code with internal modules.
- Answer: False. While modules (ES6 modules) are generally preferred for their better scoping and reusability, the
Studying these aspects of TypeScript will help deepen your understanding of the language's more complex features. If you need further clarification on any of these topics, feel free to ask!
Certainly, here are five more intermediate to advanced True/False questions regarding TypeScript:
Question: In TypeScript, the
never
type is used for variables that are always null or undefined.- Answer: False. The
never
type represents the type of values that never occur. For example, it is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns.
- Answer: False. The
Question: You can use the
unknown
type to force developers to perform type-checking before using the variable, making it safer than theany
type.- Answer: True. The
unknown
type is TypeScript's type-safe counterpart toany
. A value of typeunknown
cannot be used until its type has been narrowed to a more specific type.
- Answer: True. The
Question: When using module augmentation in TypeScript, you can only add new members to an existing module and cannot modify existing ones.
- Answer: False. Module augmentation allows you to add new members to an existing module, but it also allows for the modification of existing module declarations through interface merging.
Question: Type assertions in TypeScript can change the actual type of a variable at runtime.
- Answer: False. Type assertions are purely a compile-time construct and have no impact on the runtime type of a variable. They are a way for developers to provide hints to the compiler on the type of an object.
Question: TypeScript allows abstract classes to contain both complete (non-abstract) and incomplete (abstract) members.
- Answer: True. Abstract classes in TypeScript can have a mix of abstract and concrete (fully implemented) members. The abstract members must be implemented by non-abstract subclasses.
These questions are designed to challenge and ensure a strong understanding of some of the more sophisticated features of TypeScript. Keep studying, and don't hesitate to ask if you have any doubts or need further explanations!