|
| 1 | +/** |
| 2 | + * Description |
| 3 | + * |
| 4 | + * @param {number} a Number |
| 5 | + * @param {number} b Number |
| 6 | + * @returns {number} Sum of numbers |
| 7 | + */ |
| 8 | +function sum(a, b) { |
| 9 | + return a + b; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * @typedef {object} SpecialType1 Creates a new type named 'SpecialType' |
| 14 | + * @property {string} prop1 A string property of SpecialType |
| 15 | + * @property {number} prop2 A number property of SpecialType |
| 16 | + * @property {number} [number=666] prop3 An optional number property of SpecialType |
| 17 | + */ |
| 18 | + |
| 19 | +/** @type {SpecialType1} */ |
| 20 | +const specialTypeObject1 = { prop1: "foo", prop2: 123 }; |
| 21 | + |
| 22 | +/** |
| 23 | + * @returns {{ a: string, b: number }} Returned value |
| 24 | + */ |
| 25 | +function ab() { |
| 26 | + return { a: "a", b: 12 }; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * @returns {PromiseLike<string>} Promised value. |
| 31 | + */ |
| 32 | +function ps() { |
| 33 | + return Promise.resolve("test"); |
| 34 | +} |
| 35 | + |
| 36 | +/** @type {Map<string, () => Promise<string>>} */ |
| 37 | +const pendingIdleTasks = new Map(); |
| 38 | + |
| 39 | +/** |
| 40 | + * @type {string} |
| 41 | + */ |
| 42 | +const string = "Hello world"; |
| 43 | + |
| 44 | +/** |
| 45 | + * @type {(string | boolean)} |
| 46 | + */ |
| 47 | +const sb1 = true; |
| 48 | + |
| 49 | +/** |
| 50 | + * @type {string | boolean} |
| 51 | + */ |
| 52 | +const sb2 = "hello"; |
| 53 | + |
| 54 | +/** @type {number[]} */ |
| 55 | +const ns = [1, 2, 3]; |
| 56 | +/** @type {Array.<number>} */ |
| 57 | +const nds = [1, 2, 3]; |
| 58 | +/** @type {Array<number>} */ |
| 59 | +const nas = [1, 2, 3]; |
| 60 | + |
| 61 | +/** @type {{ a: string, b: number }} */ |
| 62 | +const var9 = { a: "str", b: 1 }; |
| 63 | + |
| 64 | +/** |
| 65 | + * A map-like object that maps arbitrary `string` properties to `number`s. |
| 66 | + * |
| 67 | + * @type {Object.<string, number>} |
| 68 | + */ |
| 69 | +const stringToNumber = { test: 123 }; |
| 70 | + |
| 71 | +/** @type {Object.<number, object>} */ |
| 72 | +const arrayLike = []; |
| 73 | + |
| 74 | +arrayLike[1] = { foo: "bar" }; |
| 75 | + |
| 76 | +/** @type {function(string, boolean): number} Closure syntax */ |
| 77 | +const sbn = (one, two) => (two ? 1 : Number.parseInt(one, 10)); |
| 78 | +/** @type {(s: string, b: boolean) => number} TypeScript syntax */ |
| 79 | +const sbn2 = (one, two) => (two ? 1 : Number.parseInt(one, 10)); |
| 80 | +/** @type {(s: string, b?: boolean) => number} TypeScript syntax */ |
| 81 | +const sbn3 = (one, two) => (two ? 1 : Number.parseInt(one, 10)); |
| 82 | + |
| 83 | +/** @type {Function} */ |
| 84 | +const fn7 = () => "string"; |
| 85 | + |
| 86 | +/** |
| 87 | + * @type {*} - can be 'any' type |
| 88 | + */ |
| 89 | +const star = {}; |
| 90 | +/** |
| 91 | + * @type {?} - unknown type (same as 'any') |
| 92 | + */ |
| 93 | +const question = null; |
| 94 | + |
| 95 | +/** |
| 96 | + * @type {number | string} |
| 97 | + */ |
| 98 | +const numberOrString = Math.random() < 0.5 ? "hello" : 100; |
| 99 | +const typeAssertedNumber = /** @type {number} */ (numberOrString); |
| 100 | + |
| 101 | +/** @enum {number} */ |
| 102 | +const JSDocState = { |
| 103 | + BeginningOfLine: 0, |
| 104 | + SawAsterisk: 1, |
| 105 | + SavingComments: 2, |
| 106 | +}; |
| 107 | + |
| 108 | +/** |
| 109 | + * @param {string} p1 A string param. |
| 110 | + * @param {string} [p2] p2 An optional param (Closure syntax) |
| 111 | + * @param {string} [p3] Another optional param (JSDoc syntax). |
| 112 | + * @param {string} [p4="test"] An optional param with a default value |
| 113 | + * @returns {string} This is the result |
| 114 | + */ |
| 115 | +function stringsStringStrings(p1, p2, p3, p4 = "test") { |
| 116 | + return p1 + p2 + p3 + p4; |
| 117 | +} |
| 118 | + |
| 119 | +class MyClass { |
| 120 | + /** |
| 121 | + * @param {string} data String |
| 122 | + */ |
| 123 | + constructor(data) { |
| 124 | + // property types can be inferred |
| 125 | + this.name = "foo"; |
| 126 | + |
| 127 | + // or set explicitly |
| 128 | + /** @type {string | null} */ |
| 129 | + this.title = null; |
| 130 | + |
| 131 | + // or simply annotated, if they're set elsewhere |
| 132 | + /** @type {number} */ |
| 133 | + this.size = 0; |
| 134 | + |
| 135 | + this.initialize(data); // Should error, initializer expects a string |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @param {string} value String |
| 140 | + */ |
| 141 | + initialize = function (value) { |
| 142 | + this.size = value.length; |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * @template T |
| 148 | + * @extends {Set<T>} |
| 149 | + */ |
| 150 | +class SortableSet extends Set { |
| 151 | + // ... |
| 152 | +} |
| 153 | + |
| 154 | +export default { |
| 155 | + sum, |
| 156 | + specialTypeObject1, |
| 157 | + ab, |
| 158 | + ps, |
| 159 | + pendingIdleTasks, |
| 160 | + string, |
| 161 | + sb1, |
| 162 | + sb2, |
| 163 | + ns, |
| 164 | + nds, |
| 165 | + nas, |
| 166 | + var9, |
| 167 | + stringToNumber, |
| 168 | + arrayLike, |
| 169 | + sbn, |
| 170 | + sbn2, |
| 171 | + fn7, |
| 172 | + star, |
| 173 | + question, |
| 174 | + typeAssertedNumber, |
| 175 | + sbn3, |
| 176 | + JSDocState, |
| 177 | + stringsStringStrings, |
| 178 | + MyClass, |
| 179 | + SortableSet, |
| 180 | +}; |
0 commit comments