javascript - การใช้สัญลักษณ์ ES6 กับ typescript
ฉันกำลังพยายามรันโค้ดง่ายๆ บรรทัดนี้ —
let FUNJECTOR_KEY = Symbol.for('funjector')
แต่ฉันยังคงได้รับข้อผิดพลาด — ไม่สามารถหาชื่อ 'Symbol' ได้ ฉันพึ่งเริ่มใช้ TypeScript ดังนั้นฉันไม่แน่ใจว่าต้องรวมอะไรเพิ่มเติมหรือไม่?
ในกรณีของฉันฉันไม่ต้องการใช้ polyfill ตามที่อธิบายไว้ที่นี่ — การใช้สัญลักษณ์ ES-6 ใน TypeScript
คำตอบ #1
ตัวคอมไพเลอร์ TypeScript จะแปลง TS เป็น JS. TSC ไม่สามารถหาการประกาศของ Symbol ในโหมด es5 ดังนั้นข้อผิดพลาดของคุณเกิดขึ้นเฉพาะเวลาคอมไพล์เท่านั้น คุณไม่จำเป็นต้องใช้ polyfill สำหรับเวลารันไทม์
ในการแก้ปัญหานี้ คุณสามารถเปลี่ยนเป้าหมายการคอมไพล์ของคุณเป็น es6 เพื่อให้ Symbol ถูกกำหนดในไลบรารีมาตรฐาน หรือคุณสามารถเพิ่มคำนิยามด้วยตนเอง (source)
declare class Symbol {
/** Returns a string representation of an object. */
toString(): string;
/** Returns the primitive value of the specified object. */
valueOf(): Object;
/**
* Returns a new unique Symbol value.
* @param description Description of the new Symbol object.
*/
constructor(description?: string);
/**
* Returns a Symbol object from the global symbol registry matching the given key if found.
* Otherwise, returns a new symbol with this key.
* @param key key to search for.
*/
static for(key: string): Symbol;
/**
* Returns a key from the global symbol registry matching the given Symbol if found.
* Otherwise, returns a undefined.
* @param sym Symbol to find the key for.
*/
static keyFor(sym: Symbol): string;
}
// Well-known Symbols
declare module Symbol {
/**
* A method that determines if a constructor object recognizes an object as one of the
* constructor’s instances.Called by the semantics of the instanceof operator.
*/
const hasInstance: Symbol;
/**
* A Boolean value that if true indicates that an object should be flatten to its array
* elements by Array.prototype.concat.
*/
const isConcatSpreadable: Symbol;
/**
* A Boolean value that if true indicates that an object may be used as a regular expression.
*/
const isRegExp: Symbol;
/**
* A method that returns the default iterator for an object.Called by the semantics of the
* for-of statement.
*/
const iterator: Symbol;
/**
* A method that converts an object to a corresponding primitive value.Called by the
* ToPrimitive abstract operation.
*/
const toPrimitive: Symbol;
/**
* A String value that is used in the creation of the default string description of an object.
* Called by the built- in method Object.prototype.toString.
*/
const toStringTag: Symbol;
/**
* An Object whose own property names are property names that are excluded from the with
* environment bindings of the associated objects.
*/
const unscopables: Symbol;
}
ข้อควรระวัง: การตรวจสอบประเภทบางอย่างไม่ทำงานกับ symbol ตัวอย่างเช่น คุณไม่สามารถประกาศคุณสมบัติที่คำนวณเป็น symbol บน interface ได้
