哪一個關鍵字用來定義常數 enum? A. enum B. const enum ✅ C. static enum D. readonly enum
解析:const enum 在編譯時會被內聯,效能更好。
下面哪種是交叉型別(Intersection Types)的正確語法? A. type Person = { name: string } && { age: number }; B. type Person = { name: string } | { age: number }; C. type Person = { name: string } & { age: number }; ✅ D. type Person = { name: string, age: number };
解析:交叉型別用 &,代表合併兩個型別。
下列哪個是泛型函式的正確定義? A. function identity<T>(arg: T): T { return arg; } ✅ B. function identity(arg: T): T { return arg; } C. function identity<T>(T arg) { return arg; } D. function identity(arg) { return arg; }
進階型別系統
Partial<T> 工具型別的功能是什麼? A. 把 T 所有屬性變成必填 B. 把 T 所有屬性變成可選 ✅ C. 移除 T 的所有屬性 D. 把 T 轉換成陣列
下列哪一個是索引型別查詢的正確用法? A. type Keys = keyof Person; ✅ B. type Keys = valueof Person; C. type Keys = indexof Person; D. type Keys = typeof Person;
never 型別的用途是什麼? A. 表示一個函式永遠不會回傳 ✅ B. 表示一個變數可以是任何型別 C. 表示一個變數為空 D. 表示一個變數尚未初始化
下列哪個範例正確使用 as const? A. const arr = [1, 2, 3] as const; ✅ B. let arr = [1, 2, 3] as const; C. const arr as const = [1, 2, 3]; D. const arr = as const [1, 2, 3];
arr[0] = 99; // ❌ 錯誤:Cannot assign to '0' because it is a read-only property arr.push(4); // ❌ 錯誤:Property 'push' does not exist on type 'readonly [1, 2, 3]'
哪一種方式可以防止物件被修改? A. readonly ✅ B. private C. const D. static
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
functionfn() { return123; }
type Result = ReturnType<typeof fn>; // Result = number
哪個選項正確使用了條件型別? A. type IsString<T> = T extends string ? true : false; ✅ B. type IsString<T> = string extends T ? true : false; C. type IsString<T> = T ? true : false; D. 以上皆非
下列哪個型別表示「除了 T 以外的所有型別」? A. Exclude<U, T> ✅ B. Extract<U, T> C. Omit<U, T> D. Partial<T>
以下程式碼輸出什麼?
1 2
enum Color { Red, Green, Blue } console.log(Color[0]);
A. 0 B. 'Red' ✅ C. '0' D. undefined
TypeScript 編譯器的輸出是什麼? A. .ts 檔案 B. .d.ts 檔案 C. .js 檔案 ✅ D. .tsconfig 檔案
哪個修飾子可以讓類別屬性在繼承中可用,但在外部不可存取? A. private B. protected ✅ C. public D. readonly
下列哪個不是型別別名(Type Alias)的用途? A. 為複雜型別取別名 B. 定義物件形狀 C. 建立聯合型別 D. 被類別實作 ✅
TypeScript 中模組預設匯出的正確寫法是 A. export = MyClass; B. export default MyClass; ✅ C. module.exports = MyClass; D. exports.MyClass = MyClass;
下列哪個是非同步函式的正確寫法? A. function getData(): Promise<string> {} B. async function getData(): Promise<string> {} ✅ C. function getData(): string {} D. await function getData() {}