ES6λ Object, Boolean, Error, Map, Promise ..etcμ κ°μ μΌλΆ λ€μ΄ν°λΈ κ°μ²΄λ₯Ό νμ ν΄λμ€λ‘ μ§μ ν©λλ€. μ΄λ¬ν κ΅¬μ‘°κ° lib.d.tsμμ λͺ¨λΈλ§λλ λ°©μμ varμ μΈν°νμ΄μ€μ μμΌλ‘ μ μλλ―λ‘ μλΈ ν΄λμ€κ° λΆκ°λ₯ν©λλ€.
ES6 μ¬μ μΉμ 19.5.1μμ :
Error μμ±μλ νμ ν΄λμ€κ°λλλ‘ μ€κ³λμμ΅λλ€. ν΄λμ€ μ μΈμ extends μ μ κ°μΌλ‘ μ¬μ©ν μ μμ΅λλ€. μ§μ λ Error λΉν€μ΄λΉμ΄λ₯Ό μμνλ €λ νμ ν΄λμ€ μμ±μμλ νμ ν΄λμ€ μΈμ€ν΄μ€λ₯Ό μ΄κΈ°ννλ Error μμ±μμ λν μνΌ νΈμΆμ΄ ν¬ν¨λμ΄μΌν©λλ€.
νμ¬ "ν΄λμ€λ λ€λ₯Έ ν΄λμ€ λ§ νμ₯ ν μ μμ΅λλ€"μ€λ₯κ° λ°μν©λλ€.
class NotImplementedError extends Error {
constructor() {
super("Not Implemented");
}
}
μ΄κ²μ νμ ν΄λμ€κ° λ ν¨μλ₯Ό λ ΈμΆνλ js λΌμ΄λΈλ¬λ¦¬μ μΌλ°μ μΈ λ¬Έμ μ λλ€. λ΄ λΌμ΄λΈλ¬λ¦¬κ° Foo μμ±μλ₯Ό λ΄λ³΄λ΄κ³ Fooμ μ μ¬ν μΈν°νμ΄μ€λ₯Ό ꡬννλ κ°μ²΄λΏλ§ μλλΌ Fooμ νμ ν΄λμ€ μΈμ€ν΄μ€λ₯Ό μμ ν κ²μΌλ‘ μμνλ κ²½μ° νμ¬μ΄λ₯Ό νμ© ν λ°©λ²μ΄ μμ΅λλ€.
λλ λ§ν μ μκ³ μΆλ€
class Error;
κ·Έλ¬ν λͺ λ Ήλ¬Έμλ μλ° μ€ν¬λ¦½νΈκ° μμ±λμ§ μκ³ Typescriptμ κΈ°νΈ ν μ΄λΈ λ§ μ λ°μ΄νΈλ©λλ€.
μΌλ°μ μΈ κ²½μ°μλ ν΄λμ€ μ μΈμ λ³Έλ¬Έμ μΈλ―Έμ½λ‘ μΌλ‘ λ체νμ¬ λ€μκ³Ό κ°μ κ²μ νμ©νκ³ μΆμ΅λλ€.
class Foo<X, Y> extends Bar<X> implements Baz<Y>;
μ΄λ λΌμ΄λΈλ¬λ¦¬κ° Foo.prototypeμ΄ Bar.prototypeμ μΈμ€ν΄μ€μμ μ΄λ―Έ 보μ₯νκ³ Baz μΈν°νμ΄μ€λ₯Ό ꡬν ν¨μ μλ―Έν©λλ€.
@metaweta λλ κ·Έκ²μ΄ μ½κ°μ μ€λ³΅μ λ§λ€ μ μλ€κ³ μκ°ν©λλ€.
interface Foo {
}
class Foo; // Foo is now a class!
interface Bar extends Foo {
}
class Bar extends Foo; // Two `extends Foo`s.
interface X {
}
class X extends Foo; // Hmm?
interface Y extends Foo {
}
class Y; // Hmm?
μλΈ ν΄λ μ± κ°λ₯ν μΈν°νμ΄μ€κ° κ°λ°©ν ν΄λμ€λ‘ μλνκΈ° λλ¬Έμ μ¬μ ν μ μ¬λ ₯μ΄ μμ΅λλ€. # 819
@metaweta 첫 λ²μ§Έ κ²½μ°λ μ°λΉμΈνΈ ν΄λμ€ μ μΈμ μν΄ μ²λ¦¬λμ΄μΌν©λλ€. μ
// myLib.d.ts
declare class Error {
}
// no code emitted here for this.
// myfile.ts
// Error is subclassable,
class MyError extends Error {
}
λ λ²μ§Έ κ²½μ°μλ λ€λ₯Έ μ μ λ¬Έμ λ₯Ό μ μΆν©λλ€.
λ€μμ ES6 μ¬μμ "μλΈ ν΄λμ€ κ°λ₯"κ°μ²΄μ μ 체 λͺ©λ‘μ λλ€. νμ¬ μ΄λ€μ λͺ¨λ μΈν°νμ΄μ€ / var μμΌλ‘ μ μλ©λλ€.
λ¬Έμ μ :
κ°λ₯ν ν΄κ²°μ± :
@mhegazy κ°μ¬ν©λλ€. μ°λΉμΈνΈ ν΄λμ€ μ μΈ κ΅¬μ‘°λ₯Ό μμ§ λͺ»νμ΅λλ€.
μ΄λ° μμΌλ‘ Error ν΄λμ€λ₯Ό νμ₯ ν λ (μ°λΉμΈνΈ ν΄λμ€ μ μ μ¬μ©) ExtendedErrorλ₯Ό λμ§λ©΄ μ€ν μΆμ μ΄ μμ±λκ±°λ λ©μμ§κ° μ λλ‘ ν λΉλμ§ μμ΅λλ€.
declare class Error {
constructor(message?: string);
}
class ExtendedError extends Error {
constructor(message?: string) {
super(message + " extended");
}
}
//throw new Error("Test");
throw new ExtendedError("Test");
@unional μ΄κ²μ μμ§ λ¬Έμ μ²λΌ 보μ λλ€. ES6 μ¬μμ λ°λΌ μλν©λλ€. μμ§μ ES6κ° λΉμ€λμμΌλ―λ‘ μ΄μ μ΄λ₯Ό μμ ν΄μΌν©λλ€.
λλ κ·Έκ²μ μλμν¬ μ μμλ€. μλ μ€μ΄λ js μ½λμμ Error.captureStackTrace νΈμΆμ΄ μμ§λ§ μ€λ₯ μ μΈμ μ€λ₯κ° μκΈ° λλ¬Έμ tsμμ ꡬνν λ μ κ±°ν©λλ€.
μμ μ½λκ° μμ΅λλ€.
innerExceptionμλ°λ κ²μ C #κ³Ό μ μ¬ν μμΈμ
λλ€.
declare class Error {
public name:string;
public message:string;
public stack:string;
constructor(message?:string);
static captureStackTrace(error: Error, constructorOpt: any);
}
class Exception extends Error {
public innerException: Error;
constructor(message?: string, innerException?: Error|string) {
// Guard against throw Exception(...) usage.
if (!(this instanceof Exception)) return new Exception(message, innerException);
super();
if (typeof Error.captureStackTrace === 'function') {
//noinspection JSUnresolvedFunction
Error.captureStackTrace(this, arguments.callee);
}
this.name = "Exception";
if (innerException) {
if (innerException instanceof Error) {
this.innerException = innerException;
this.message = message + ", innerException: " + this.innerException.message;
}
else if (typeof innerException === "string") {
this.innerException = new Error(innerException);
this.message = message + ", innerException: " + this.innerException.message;
}
else {
this.innerException = innerException;
this.message = message + ", innerException: " + this.innerException;
}
}
else {
this.message = message;
}
}
}
# 3516μΌλ‘ μμ λμμ΅λλ€. μ΄μ ν΄λμ€λ μμ±μ ν¨μ μ νμ μμ ννμμ νμ₯ ν μ μμ΅λλ€.
μ΄ μμ μ¬νμ΄ μ 곡 λ TypeScriptμ 릴리μ€λ 무μμ
λκΉ?
1.5.3 λ° 1.5.4μ λ¦΄λ¦¬μ€ λ μ΄λΈμ μ μνκ² νμΈνλλ° μμ§ λ°°μ‘λμ§ μμ κ² κ°μΌλ©° νμ¬λ‘μλ λ§μ€ν°μμλ§ μ 곡λ©λλ€.
νΈμ§νλ€:
μ£μ‘ν©λλ€. νμ¬ μ΄μ ν "TypeScript 1.6"μΌλ‘ νμλ λ²κ·Έλ₯Ό λ°κ²¬νμ΅λλ€.
μμ ν΄ μ£Όμ μ κ°μ¬ν©λλ€!
@kostrse npm install -g typescript@next
λ₯Ό μ€ννμ¬ TypeScript 1.6μ μΌκ° 릴리μ€λ₯Ό μ¬μ©ν΄ λ³Ό μ μμ΅λλ€.
μλ νμΈμ,
Typescript 1.6.2λ₯Ό μ¬μ©νκ³ μμΌλ©° lib.es6.d.tsλ μ€λ₯ (λ° λ°°μ΄
1.6.2μμ μ΄λ―Έ μμ λμμ΅λκΉ?
건배!
@jpsfs μμ μ¬νμ https://github.com/Microsoft/TypeScript/issues/1168#issuecomment -112955503 μμ @ahejlsbergκ° μΈκΈ
μμ¦ Error
ν΄λμ€λ₯Ό νμ₯ ν μμλ κΈ°λ₯λ μμ§λ§, λ
Έλμμ μ¬μ ν λΆνΈ ν¨μ κ²ͺκ³ μμ΅λλ€. μλ₯Ό λ€μ΄ "error.stack"μ λ¬Έμ κ° μμ΅λλ€. throw new Error(...)
νλ©΄ error.stack
λ₯Ό λ°μ§λ§ throw new CustomError()
μ λΌμ. κ·Έκ²μ ν΄κ²°νκΈ° μν΄ λλμ΄ νΈλ¦μ μ¬μ©νλλ‘ κ°μνλ€.
export class HttpError extends Error {
httpCode: number;
message: string;
constructor(httpCode: number, message?: string) {
super();
if (httpCode)
this.httpCode = httpCode;
if (message)
this.message = message;
this.stack = new Error().stack;
}
}
κ°μ₯ μ μ©ν λκΈ
μμ¦
Error
ν΄λμ€λ₯Ό νμ₯ ν μμλ κΈ°λ₯λ μμ§λ§, λ Έλμμ μ¬μ ν λΆνΈ ν¨μ κ²ͺκ³ μμ΅λλ€. μλ₯Ό λ€μ΄ "error.stack"μ λ¬Έμ κ° μμ΅λλ€.throw new Error(...)
νλ©΄error.stack
λ₯Ό λ°μ§λ§throw new CustomError()
μ λΌμ. κ·Έκ²μ ν΄κ²°νκΈ° μν΄ λλμ΄ νΈλ¦μ μ¬μ©νλλ‘ κ°μνλ€.