// Optional parameter
function foo(x?: number) {
console.log("x : "+ x);
}
foo();
foo(6);
function functionName(par1: number, par2?: number) {
}
function multiply(a: number, b: number, c?: number): number {
if (typeof c !== 'undefined') {
return a * b * c;
}
return a * b;
}
/*
**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") {}
}
interface SquareConfig {
color?: string; // optional
width: number; // not optional
}
// Optional type parameters
private logData<T, S = {}>(operation: string, responseData: T, requestData?: S) {
// your implementation here
}
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"