xxxxxxxxxx
function multiply(a: number, b: number, c?: number): number {
if (typeof c !== undefined) {
return a * b * c;
}
return a * b;
}
xxxxxxxxxx
// Optional parameter
function foo(x?: number) {
console.log("x : "+ x);
}
foo();
foo(6);
xxxxxxxxxx
function newsArticle(options: newsArticleOptions) {
const {
showHeadline = true,
showDate = true,
articleType = "World"
} = options;
}
// Use like this
newsArticle({ articleType: "Tech" });
// Of course, don't write JavaScript without TypeScript
interface newsArticleOptions {
showHeadline?: boolean
showDate?: boolean
articleType?: articleType
}
type articleType = "Tech" | "World" | "Sports"
xxxxxxxxxx
function multiply(a: number, b: number, c?: number): number {
if (typeof c !== 'undefined') {
return a * b * c;
}
return a * b;
}
xxxxxxxxxx
function greet(name: string, greeting: string = "Hello") {
console.log(`${greeting}, ${name}!`);
}
greet("John"); // Output: Hello, John!
greet("Sarah", "Hi"); // Output: Hi, Sarah!
xxxxxxxxxx
// Optional type parameters
private logData<T, S = {}>(operation: string, responseData: T, requestData?: S) {
// your implementation here
}
xxxxxxxxxx
/*
**DESCRIPTION**
Default-initialized parameters that come after all required
parameters are treated as optional, and just like
optional parameters, can be omitted when calling their
respective function
*/
export class Test {
constructor(private foo: string = "foo", private bar: string = "bar") {}
}
xxxxxxxxxx
# credit to Stack Overflow user in the source link
# shell command or ! + command in ipython
jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb