Skip to content

Instantly share code, notes, and snippets.

@vvakame
Last active September 6, 2015 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvakame/2c0ec905b78125319460 to your computer and use it in GitHub Desktop.
Save vvakame/2c0ec905b78125319460 to your computer and use it in GitHub Desktop.
TypeScript 1.6.0-beta 変更点 ref: http://qiita.com/vvakame/items/072fa78f9fe496edd1f0
abstract class AbstractTest {
}
// Cannot create an instance of the abstract class 'AbstractTest'.
// let obj = new AbstractTest();
// OK!
let obj = new class extends AbstractTest {} ();
// メソッドもabstractにできる
abstract class Test {
abstract hi(): string;
sayHi() {
console.log(this.hi());
}
}
new (class extends Test {
hi(){
return "Hi!";
}
})().sayHi();
// 以下2つは等価
let obj1 = <any>{};
let obj2 = {} as any;
interface TestConstructor {
new (): Test;
}
interface Test {
str: string;
}
declare let Test: TestConstructor;
class Sample extends Test {
num: number;
}
let obj = new Sample();
obj.str; // OK
obj.num; // OK
obj.bool; // Property 'bool' does not exist on type 'Sample'.
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = 1;
export default 1;
// https://github.com/Microsoft/TypeScript/pull/3516 より翻案
class ThingA {
getGreeting() { return "Hello from A"; }
}
class ThingB {
getGreeting() { return "Hello from B"; }
}
class Test extends (() => Math.random() > 0.5 ? ThingA : ThingB)() {
sayHello() {
console.log(this.getGreeting());
}
}
// Hello from A ✕ 3 または Hello from B ✕ 3 が表示される
new Test().sayHello();
new Test().sayHello();
new Test().sayHello();
// --target es6でコンパイルするか、適当にPromiseを解決する
// DefinitelyTypedからes6-promise持ってくるか、TypeScript同梱のlib.es6.d.tsを参照する
type PromiseValue<T> = Promise<T> | T;
function addOne(num: PromiseValue<number>) {
return Promise.resolve(num).then(num => num + 1);
}
addOne(1).then(v => console.log(v));
addOne(addOne(1)).then(v => console.log(v));
// 2 と 3 が表示される
// 以下はちゃんとコンパイルエラーになる
// addOne("test").then(v => console.log(v));
// addOne(Promise.resolve("test")).then(v => console.log(v));
interface A {
str: string;
}
interface B {
num: number;
}
type C = A & B; // intersection!!
// Bと互換性がない。numがない。
// let objA: C = { str: "A" };
// Aと互換性がない。strがない。
// let objB: C = { num: 2 };
// OK
let objC: C = { str: "A", num: 2 };
function returnTestClass() {
return class Test {};
}
let TestA = returnTestClass();
let TestB = returnTestClass();
// false と表示される
console.log(TestA === TestB);
let objA = new TestA();
// true と表示される
console.log(objA instanceof TestA);
// false と表示される
console.log(objA instanceof TestB);
{
class Test {
hi() { return "Hi!"; }
}
console.log(new Test().hi());
}
{
class Test {
hello() { return "Hello!"; }
}
console.log(new Test().hello());
}
// error TS2304: Cannot find name 'Test'.
// new Test();
// ambient class だけ!
declare class Test {
str: string;
}
interface Test {
num: string;
}
let obj = new Test();
obj.str;
obj.num;
// 足りてない! (今までもダメだった)
let objA: {str: string;} = {};
// 多すぎる! (1.6.0-betaからダメ) オブジェクトリテラルは既知のプロパティのみ可だよ!
// Object literal may only specify known properties, and 'num' does not exist in type '{ str: string; }'.
let objB: {str: string;} = { str: "", num: 1 };
// 一旦別の変数に入れてからなら、プロパティが多すぎても怒られない。
let objC: {str: string;};
let objD = { str: "", num: 1 };
objC = objD;
interface FooOptions {
fileName?: string;
checkBar?: boolean;
}
declare function foo(opts: FooOptions): void;
// fileName の大文字小文字を間違えている!
// Object literal may only specify known properties, and 'filename' does not exist in type 'FooOptions'.
foo({
filename: "vvakame.txt",
checkBar: true
});
{
"compilerOptions": {
"module": "commonjs",
"target": "es3",
"noImplicitAny": false,
"outDir": "built",
"rootDir": ".",
"sourceMap": false
},
"exclude": [
"node_modules"
]
}
// 今までの書き方
declare namespace angular.resource1 {
interface ResourceProvider {
create<T extends Resource<any>>(): T;
}
interface Resource<T> {
$insert(): T;
}
var $resource: ResourceProvider;
}
// 上の定義を使ってみる
namespace sample1 {
interface Sample {
str: string;
}
// SampleResource の作り方をよく知っていないといけない…!どういうトリックかわかるだろうか?
interface SampleResource extends Sample, angular.resource1.Resource<Sample> {}
let $obj = angular.resource1.$resource.create<SampleResource>();
$obj.str = "test";
let obj = $obj.$insert();
console.log(obj.str);
}
// これからの書き方
declare namespace angular.resource2 {
interface ResourceProvider {
create<T>(): T & Resource<T>;
}
interface Resource<T> {
$insert(): T;
}
var $resource: ResourceProvider;
}
// 上の定義を使ってみる
namespace sample2 {
interface Sample {
str: string;
}
// 超簡単…!!
let $obj = angular.resource2.$resource.create<Sample>();
$obj.str = "test";
let obj = $obj.$insert();
console.log(obj.str);
}
interface TextNode {
text: string;
}
interface NumberNode {
num: number;
}
function isTextNode(node: any): node is TextNode {
return node && typeof node.text !== "undefined";
}
let node: TextNode | NumberNode;
node = { text: "test" };
if (isTextNode(node)) {
// TextNode に絞りこまれている!
console.log(node.text);
}
node = { num: 1 };
if (isTextNode(node)) {
// false なのでここに到達しない
console.log(node.text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment