ambient: 環境
JavaScriptコードを生成せず、型推論器にだけ情報を渡すのに使われるのが アンビエント宣言 (または環境宣言; ambient declaration) です。 declare のついた宣言がアンビエント宣言になります。 (declare なのに呼び名が「アンビエント」なのは不思議ですが、declareは「宣言する」という意味なので仕方なさそうです)
ref. https://typescript-jp.gitbook.io/deep-dive/future-javascript/destructuring
const rect = {width: 100, height: 200, depth: 300};
const {width, height, depth} = rect;
console.log(width, height, depth);
// 100, 200, 300
分割を使用して構造体の深いデータを取得
-- https://typescript-jp.gitbook.io/deep-dive/future-javascript/destructuring
const page = {
title: 'Hello World',
content: {
catchCopy: 'HELLO WORLD',
lead: 'Welcome to my page.',
body: 'This is content'
}
}
var {content: {catchCopy}} = page;
console.log(catchCopy);
// HELLO World
console.log(content);
// ERR content is not defined
var { content } = page;
console.log(content);
//
{
body: "This is content",
catchCopy: "HELLO WORLD",
lead: "Welcome to my page."
}
const page = {
title: 'Hello World',
content: {
catchCopy: 'HELLO WORLD',
lead: 'Welcome to my page.',
body: 'This is content'
}
}
var {content} = page;
console.log(content);
// {
// "catchCopy": "HELLO WORLD",
// "lead": "Welcome to my page.",
// "body": "This is content"
// }
content
をfoo
に代入。
const page = {
title: 'Hello World',
content: {
catchCopy: 'HELLO WORLD',
lead: 'Welcome to my page.',
body: 'This is content'
}
}
const {content: foo} = page;
console.log(foo);
// {
// "catchCopy": "HELLO WORLD",
// "lead": "Welcome to my page.",
// "body": "This is content"
// }
class Hello {
constructor(msg: string) {
this.message(msg)
}
message(msg: string) {
console.log(msg);
}
}
class Foo {
msg: string;
constructor(msg:string) {
this.msg = msg;
new Hello(this.msg);
}
}
const foo = new Foo('hello');
console.log(foo)
// 結果
/*
"hello"
Foo: {
"msg": "hello"
}
*/
class Hello {
msg: string
constructor(msg: string) {
this.msg = msg;
}
message() {
console.log(this.msg)
}
}
new Hello("Hello world").message();
/*
結果
"Hello world"
*/
construct:構成する,構成物