This is what I did so far. I want to document the method
lufthansa.book
How should I approach It ? Should I document It inside the Object like below? Or In the@typedef {Object} Airline
/**
* This is a predefinition of the method that is inside the Object
* It will be used as the type at @property {Type} for the method
* BookMethod will be used the type of lufthansa.book
* @typedef {Function} BookMethod
* @returns {void}
*/
/**
* @typedef {Object} Airline
* @property {String} airline
* @property {String} iataCode
* @property {Array} bookings The array of bookings
* @property {BookMethod} book
*/
/**
* @name lufthansa
* @type {Airline}
*/
const lufthansa = {
airline: "Lufthansa",
iataCode: "LH",
bookings: [],
/**
* @type {BookMethod}
* @param {Number} flightNum
* @param {String} name
*/
book(flightNum, name) {
console.log(`
${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
`);
},
};
lufthansa.book(2321, "Jean Steel");
The example below didn't work. If I use this vscode initiates type check message
Parameter 'flightNum' implicitly has an 'any' type, but a better type may be inferred from usage.
this is what I receive for both param if I use the approach below
/**
* This is a predefinition of the method that is inside the Object
* It will be used as the type at @property {Type} for the method
* @typedef {Function} BookMethod
* @param {Number} flightNum
* @param {String} name
* @returns {void}
*/
/**
* This predefinition for the Object
* @typedef {Object} Airline
* @property {String} airline
* @property {String} iataCode
* @property {Array} bookings The array of bookings
* @property {BookMethod} book
*/
/**
* @name lufthansa
* @type {Airline}
*/
const lufthansa = {
airline: "Lufthansa",
iataCode: "LH",
bookings: [],
book(flightNum, name) {
console.log(`
${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
`);
},
};
lufthansa.book(2321, "Jean Steel");
This is what I did so far. I want to document the method
lufthansa.book
How should I approach It ? Should I document It inside the Object like below? Or In the@typedef {Object} Airline
/**
* This is a predefinition of the method that is inside the Object
* It will be used as the type at @property {Type} for the method
* BookMethod will be used the type of lufthansa.book
* @typedef {Function} BookMethod
* @returns {void}
*/
/**
* @typedef {Object} Airline
* @property {String} airline
* @property {String} iataCode
* @property {Array} bookings The array of bookings
* @property {BookMethod} book
*/
/**
* @name lufthansa
* @type {Airline}
*/
const lufthansa = {
airline: "Lufthansa",
iataCode: "LH",
bookings: [],
/**
* @type {BookMethod}
* @param {Number} flightNum
* @param {String} name
*/
book(flightNum, name) {
console.log(`
${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
`);
},
};
lufthansa.book(2321, "Jean Steel");
The example below didn't work. If I use this vscode initiates type check message
Parameter 'flightNum' implicitly has an 'any' type, but a better type may be inferred from usage.
this is what I receive for both param if I use the approach below
/**
* This is a predefinition of the method that is inside the Object
* It will be used as the type at @property {Type} for the method
* @typedef {Function} BookMethod
* @param {Number} flightNum
* @param {String} name
* @returns {void}
*/
/**
* This predefinition for the Object
* @typedef {Object} Airline
* @property {String} airline
* @property {String} iataCode
* @property {Array} bookings The array of bookings
* @property {BookMethod} book
*/
/**
* @name lufthansa
* @type {Airline}
*/
const lufthansa = {
airline: "Lufthansa",
iataCode: "LH",
bookings: [],
book(flightNum, name) {
console.log(`
${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
`);
},
};
lufthansa.book(2321, "Jean Steel");
Share
Improve this question
edited Dec 12, 2022 at 16:01
Tohirul Islam
asked Dec 12, 2022 at 15:24
Tohirul IslamTohirul Islam
4043 silver badges11 bronze badges
12
- 1 I find it a bit unclear what is your problem exactly, what initiates a type check and on what? please add mode details. It seems you expect javascript to behave like typescript regarding custom types. Is it the case in VSCode? If what you are looking for is a behavious specific to VSCode, you should add its tag IMO – Kaddath Commented Dec 12, 2022 at 15:43
- I want to know how to document a method. In this case the book method that inside the lufthansa Object. I used typedef to predefine the Object and it's properties. Method is a function inside an Object so how can I predefine in at typedef – Tohirul Islam Commented Dec 12, 2022 at 15:46
-
1
Yes but what exactely are you looking for? The method
book
doesn't show on the documentation? What is the oute of both ways you tried and added to the question? What is the link to VSCode and a type check? – Kaddath Commented Dec 12, 2022 at 15:52 - it doesn't show on the documentation – Tohirul Islam Commented Dec 12, 2022 at 15:55
- 1 If I ask this it's because not everybody is familiar with the tools you may use, for example, I know JSDoc but never used VSCode, so I may be able to help you or not depending on what you are looking for. You should edit the question itself to add your precisions, and use text version of code and errors rather than use images – Kaddath Commented Dec 12, 2022 at 15:55
1 Answer
Reset to default 7There are several solutions. If you want to keep type definition separately from the code, you can write something like this:
/**
* @typedef {Object} Airline
* @property {String} airline
* @property {String} iataCode
* @property {Array} bookings The array of bookings
* @property {(flightNum: number, name: string) => void} book Book a seat on a flight
*/
/**
* @name lufthansa
* @type {Airline}
*/
const lufthansa = {
airline: "Lufthansa",
iataCode: "LH",
bookings: [],
book(flightNum, name) {
console.log(`
${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
`);
},
};
or you can go backward and infer type:
/**
* @name lufthansa
*/
const lufthansa = {
airline: "Lufthansa",
iataCode: "LH",
/**
* @description The array of bookings
* @type {Array}
*/
bookings: [],
/**
* @description Book a seat on a flight
* @param {number} flightNum
* @param {string} name
*/
book(flightNum, name) {
console.log(`
${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
`);
},
};
/**
* @typedef {typeof lufthansa} Airline
*/
lufthansa.book(1, "Jean Steel");