Typescript 面試問題

void 和 undefined 有什麼區別?

什麼是 never 類型?

下面代碼會不會報錯?怎麼解決?

const obj = {
    a: 1,
    b: 'string',
};
  
obj.c = null;

readonly 和 const 有什麼區別?

下面代碼中,foo 的類型應該如何聲明

function foo (a: number) {
 
    return a + 1;
 
}
 
foo.bar = 123;

下面代碼中,foo 的類型如何聲明

let foo = {};
  
for (let i = 0; i< 100; i++) {
    foo[String(i)] = i;
}

實現 MyInterface

interface MyInterface {
    ...
}
class Bar implements MyInterface {
    constructor(public name: string) {}
}
class Foo implements MyInterface {
    constructor(public name: string) {}
}
  
function myfn(Klass: MyInterface, name: string) {
    return new Klass(name);
}
  
myfn(Bar);
myfn(Foo);

什麼是 Abstract Class?

什麼是 class mixin, 如何實現?

typeof 關鍵詞有什麼用?

keyof 關鍵詞有什麼用?

下面代碼中,foo 的類型如何聲明

function fn(value: number): string {
    return String(value);
}
const foo = fn;

下面代碼會致使 TS 編譯失敗,如何修改 getValue 的類型聲明。

function getValue() {
    return this.value;
}
  
getValue();

下面是 vue-class-component 的部分代碼,解釋爲何會有多個 Component 函數的聲明,做用是什麼?TS 官方文檔的那一節有對應的解釋文檔?

function Component <U extends Vue>(options: ComponentOptions<U>): <V extends VueClass>(target: V) => V
function Component <V extends VueClass>(target: V): V
function Component <V extends VueClass, U extends Vue>(
 options: ComponentOptions<U> | V
): any {
  if (typeof options === 'function') {
    return componentFactory(options)
  }
  return function (Component: V) {
    return componentFactory(Component, options)
  }
}

如何聲明 foo 的類型?

const foo = new Map();
foo.set('bar', 1);

如何聲明 getProperty,以便能檢查出第八行將會出現的運行錯誤。

function getProperty(obj, key) {
    return obj[key].toString();
}
 
let x = { a: 1, b: 2, c: 3, d: 4 };
 
getProperty(x, "a");
getProperty(x, "m");

類型聲明裏 「&」和「|」有什麼做用?

下面代碼裏「date is Date」有什麼做用?

function isDate(date: any): date is Date {
  if (!date) return false;
  return Object.prototype.toString.call(date) === '[object Date]';
}

tsconfig.json 裏 --strictNullChecks 參數的做用是什麼?

interface 和 type 聲明有什麼區別?

如何完善 Calculator 的聲明。

interface Calculator {
    ...
}
 
let calcu: Calculator;
calcu(2)
  .multiply(5)
  .add(1)

「import ... from」、「 import ... = require()」 和 「import(path: string)」有什麼區別?

declare 關鍵字有什麼用?

module 關鍵字有什麼用?

如何處理才能在 TS 中引用 CSS 或者 圖片使之不報錯?

import "./index.scss";
import imgPath from "./home.png";

編寫 d.ts 來聲明下面的 js 文件

class Foo {
}
module.exports = Foo;
module.exports.Bar = 1;

namespace 和 module 有什麼區別

如何實現 module alias?編譯成 JS 可否直接運行?

import Bar from '@src/Bar';

哪些聲明類型既能夠當作 type 也能夠當作 value?

相關文章
相關標籤/搜索