Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support typescript #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ inquirer.prompt({
})
```

## Typescript

Add `inquirer-autocomplete-prompt` to *tsconfig.json/compilerOptions/types*

```json
{
"compilerOptions": {
"types": [
"inquirer-autocomplete-prompt"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this change. See the docs:

If types is specified, only packages listed will be included in the global scope. For instance:

Besides, "types": "index.d.ts", in package.json already provides the types.

]
}
}
```

Change `autocomplete` to whatever you might prefer.

### Options
Expand Down
22 changes: 22 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import inquirer, { Answers } from 'inquirer';

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is, I get this error when trying your branch:

error TS2345: Argument of type 'typeof import("./node_modules/inquirer-autocomplete-prompt/index")' is not assignable to parameter of type 'PromptConstructor'.
  Type 'typeof import("./node_modules/inquirer-autocomplete-prompt/index")' provides no match for the signature 'new (question: any, readLine: Interface, answers: Answers): PromptBase'.

10 inquirer.registerPrompt(`autocomplete`, autocomplete)

This is my tsconfig.json:

{
	"extends": "@tsconfig/node12/tsconfig.json",
	"compilerOptions": {
		"esModuleInterop": true,
		"moduleResolution": "node",
		"strict": true,
		"outDir": "./.transpiled",
		"noUncheckedIndexedAccess": true,
		"noPropertyAccessFromIndexSignature": true,
	},
	"include": [ "./" ],
}

It seems to me that this file is missing the actual export types, please verify and fix.


export default interface InquirerAutocompletePrompt {}

declare var inquirerAutocompletePrompt: InquirerAutocompletePrompt;
export = inquirerAutocompletePrompt;

I added that 👆 to the end of this file and the error changed to this 👇

error TS2345: Argument of type 'InquirerAutocompletePrompt' is not assignable to parameter of type 'PromptConstructor'.  
  Type 'InquirerAutocompletePrompt' provides no match for the signature 'new (question: any, readLine: Interface, answers: Answers): PromptBase'.

10 inquirer.registerPrompt(`autocomplete`, autocomplete)

Note how typeof import("./node_modules/inquirer-autocomplete-prompt/index") changed to InquirerAutocompletePrompt – that seems to confirm my suspicion.

declare module 'inquirer' {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To extend the inquirer interfaces, this seems more appropriate to get alignment:

Suggested change
declare module 'inquirer' {
declare namespace inquirer {

See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/inquirer/index.d.ts#L95

Note: So far, I have not typed across packages myself, so I may be missing something. This comment is really based only on my observation in @types/inquirer.

interface AutocompleteQuestionOptions<T> extends Question<T> {
suggestOnly?: boolean;
searchText?: string;
emptyText?: string;
default?: string;
pageSize?: number;
filter?(options: Array<string>): Array<string>;
validate?(line: string): boolean;
source(answersSoFar: Answers, input: string): Promise<Array<string>>;
}

export interface AutocompleteQuestion<T extends Answers = Answers> extends AutocompleteQuestionOptions<T> {
type: "autocomplete";
}

export interface QuestionMap<T extends Answers = Answers> {
autocomplete: AutocompleteQuestion<T>;
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind the final newline 😉 It's better for the diffs.

25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"files": [
"index.js"
],
"types": "index.d.ts",
"repository": {
"type": "git",
"url": "git@github.com:mokkabonna/inquirer-autocomplete-prompt.git"
Expand All @@ -22,6 +23,7 @@
},
"license": "ISC",
"devDependencies": {
"@types/inquirer": "^7.3.1",
"chai": "^4.2.0",
"coveralls": "^3.1.0",
"eslint": "^6.8.0",
Expand Down