let firstName: string = "Rutvik";
let age = 23;
let x: any = 10; x = 'hello'; // No TypeScript error console.log(x.toUpperCase()); // No TypeScript error
let y: unknown = 10; // Type assertion needed before using y as number if (typeof y === 'number') { console.log(y.toFixed(2)); }
function throwError(message: string): never { throw new Error(message); }
const names: string[] = ["Rutvik", "Rohit", "Virat"]; names.push("Bumrah"); // no error
const names: readonly string[] = ["Rutvik", "Rohit", "Virat"]; names.push("Bumrah"); // Error: Property 'push' does not exist on type 'readonly string[]'.
const numbers = [1, 2, 3]; // inferred to type number[] numbers.push(4); // no error
let ourTuple: [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here'];
let ourTuple: [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here']; //No safety in indexes from 3 ourTuple.push('This is wrong');
let ourTuple: readonly [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here']; // throws error as it is readonly ourTuple.push('Coding Hero took a day off');
interface CarTypes { brand: string, model: string, year: number } const car: CarTypes = { brand: "Tata", model: "Punch", year: 2020 };
let firstName: string = "Rutvik";
let age = 23;
let x: any = 10; x = 'hello'; // No TypeScript error console.log(x.toUpperCase()); // No TypeScript error
let y: unknown = 10; // Type assertion needed before using y as number if (typeof y === 'number') { console.log(y.toFixed(2)); }
function throwError(message: string): never { throw new Error(message); }
const names: string[] = ["Rutvik", "Rohit", "Virat"]; names.push("Bumrah"); // no error
const names: readonly string[] = ["Rutvik", "Rohit", "Virat"]; names.push("Bumrah"); // Error: Property 'push' does not exist on type 'readonly string[]'.
const numbers = [1, 2, 3]; // inferred to type number[] numbers.push(4); // no error
let ourTuple: [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here'];
let ourTuple: [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here']; //No safety in indexes from 3 ourTuple.push('This is wrong');
let ourTuple: readonly [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here']; // throws error as it is readonly ourTuple.push('Coding Hero took a day off');
interface CarTypes { brand: string, model: string, year: number } const car: CarTypes = { brand: "Tata", model: "Punch", year: 2020 };
interface CarTypes { brand: string, model: string, year?: number } const car: CarTypes = { brand: "Tata", model: "Punch" };
enum Direction { Up = 1, Down, Left, Right, } console.log(Direction.Up); // 1 console.log(Direction.Down); // 2
let firstName: string = "Rutvik";
let age = 23;
let x: any = 10; x = 'hello'; // No TypeScript error console.log(x.toUpperCase()); // No TypeScript error
let y: unknown = 10; // Type assertion needed before using y as number if (typeof y === 'number') { console.log(y.toFixed(2)); }
function throwError(message: string): never { throw new Error(message); }
const names: string[] = ["Rutvik", "Rohit", "Virat"]; names.push("Bumrah"); // no error
const names: readonly string[] = ["Rutvik", "Rohit", "Virat"]; names.push("Bumrah"); // Error: Property 'push' does not exist on type 'readonly string[]'.
const numbers = [1, 2, 3]; // inferred to type number[] numbers.push(4); // no error
let ourTuple: [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here'];
let ourTuple: [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here']; //No safety in indexes from 3 ourTuple.push('This is wrong');
let ourTuple: readonly [number, boolean, string]; // initialize correctly ourTuple = [5, false, 'Coding Hero was here']; // throws error as it is readonly ourTuple.push('Coding Hero took a day off');
interface CarTypes { brand: string, model: string, year: number } const car: CarTypes = { brand: "Tata", model: "Punch", year: 2020 };
interface CarTypes { brand: string, model: string, year?: number } const car: CarTypes = { brand: "Tata", model: "Punch" };
enum Direction { Up = 1, Down, Left, Right, } console.log(Direction.Up); // 1 console.log(Direction.Down); // 2
以上是TypeScript 面試問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!