TypeScript Functions All In One
TypeScript Functions All In One
demos
type Log = (a: any) => void;
const log: Log = console.log;
log(`typescript function`);
.d.ts
type Log = (a: any) => void;
declare const log: Log;
// function type & type alias
type Log = (...data: any[]) => void;
const log: Log = console.log;
log(`typescript function`);
Node.js & console.log
https://github.com/nodejs/node/blob/v16.4.2/lib/console.js
https://github.com/nodejs/node/blob/v16.4.2/lib/internal/console/global.js
https://github.com/nodejs/node/blob/v16.4.2/lib/internal/console/constructor.js
(method) Console.log(...data: any[]): void (+1 overload)
(method) Console.log(...data: any[]): void (+1 overload)
namespace console
var console: Console
The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() andconsole.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and process.stderr. The global console can be used without callingrequire('console').
Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
@see — source
Function Type
- function type expression / 函数类型表达式
// 显式的声明 ts function 返回值 void
function greeter(fn: (a: string) => void): void {
fn("Hello, World");
}
function printToConsole(s: string): void {
console.log(s);
}
greeter(printToConsole);
// function printToConsole(s: string): void
// function greeter(fn: (a: string) => void): void
// ts function 默认返回值 void
function greeter(fn: (a: string) => void) {
fn("Hello, World");
}
function printToConsole(s: string) {
console.log(s);
}
greeter(printToConsole);
// function printToConsole(s: string): void
// function greeter(fn: (a: string) => void): void
declare function greeter(fn: (a: string) => void): void;
declare function printToConsole(s: string): void;
function greeter1(fn: (a: string) => void) {
fn("Hello, World");
// 自动类型推断 number
return 1;
}
// declare function greeter(fn: (a: string) => void): number;
// 显式的声明 ts function 返回值 any
function greeter2(fn: (a: string) => void): any {
fn("Hello, World");
return 2;
}
// declare function greeter2(fn: (a: string) => void): any;
type alias / 类型别名
// type GreetFunction = (a) => void;
// 不声明,默认的参数类型是 any
// type GreetFunction = (a: any) => void;
// 类型别名
type GreetFunction = (a: string) => void;
function greeter(fn: GreetFunction) {
fn("Hello, World");
}
- Call Signatures / 调用签名
type DescribableFunction = {
description: string;
(arg: string): any;
};
function greeter(fn: DescribableFunction) {
console.log(`fn.description`, fn.description);
fn("Hello, World");
return 2;
}
function func(s: string): void {
console.log(`s =`, s);
}
// function is Object
func.description = 'xyz';
greeter(func);
type DescribableFunction = {
description: string;
(arg: string): any;
};
declare function greeter(fn: DescribableFunction): number;
declare function func(s: string): void;
declare namespace func {
var description: string;
}
TypeScript
Functions
https://www.typescriptlang.org/docs/handbook/functions.html
More on Functions
https://www.typescriptlang.org/docs/handbook/2/functions.html
refs
https://github.com/xgqfrms/learn-typescript-by-practice
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16227964.html
未经授权禁止转载,违者必究!