-
Notifications
You must be signed in to change notification settings - Fork 130
[ja translation] By Example #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thanks for the PR! This section of the codebase is owned by @sasurau4, @Quramy, and @Naturalclar - if they write a comment saying "LGTM" then it will be merged. |
Translation of By Example.mdtitle: Declaration Reference oneline: "How to create a .d.ts file for a module"The purpose of this guide is to show you how to write a good declaration file. The following samples are listed in order of most complexity. Object with propertiesspecification
Code example let result = myLib.makeGreeting("hello, world");
console.log("The computed greeting is:" + result);
let count = myLib.numberOfGreetings; declaration To describe the types and values that can be accessed by dot notation, declare namespace myLib {
function makeGreeting(s: string): string;
let numberOfGreetings: number;
} Overloaded functionsspecification
Code example let x: Widget = getWidget(43);
let arr: Widget[] = getWidget("all of them"); declaration declare function getWidget(n: number): Widget;
declare function getWidget(s: string): Widget[]; Reusable types (interface)specification
Code example greet({
greeting: "hello world",
duration: 4000,
}); declaration
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
declare function greet(setting: GreetingSettings): void; Reusable type (type alias)specification
Code example function getGreeting() {
return "howdy";
}
class MyGreeter extends Greeter {}
greet("hello");
greet(getGreeting);
greet(new MyGreeter()); declaration You can use type aliases to create an abbreviation of a type: type GreetingLike = string | (() => string) | MyGreeter;
declare function greet(g: GreetingLike): void; Organizing typesspecification
Code example const g = new Greeter("Hello");
g.log({ verbose: true });
g.alert({ modal: false, title: "Current Greeting" }); declaration Use namespaces to organize types. declare namespace GreetingLib {
interface LogOptions {
verbose?: boolean;
}
interface AlertOptions {
modal: boolean;
title?: string;
color?: string;
}
} You can also create namespaces nested with declarations: declare namespace GreetingLib.Options {
// GreetingLib.Options.Logで参照します
interface Log {
verbose?: boolean;
}
interface Alert {
modal: boolean;
title?: string;
color?: string;
}
} classspecification
Code example const myGreeter = new Greeter("hello, world");
myGreeter.greeting = "howdy";
myGreeter.showGreeting();
class SpecialGreeter extends Greeter {
constructor() {
super("Very special greetings");
}
} declaration To write a class or class-like object, click declare class Greeter {
constructor(greeting: string);
greeting: string;
showGreeting(): void;
} Global variablesspecification
Code example console.log("Half the number of widgets is " + foo / 2); declaration To declare a variable: /** 存在する widget の数 */
declare var foo: number; Global functionsspecification
Code example greet("hello, world"); declaration To declare a function: declare function greet(greeting: string): void; |
ref: #3
By Example.md を翻訳しました
diff: 91bf1a9