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

Minimal fixes for type declarations to resolve errors when importing transformers.js via typescript (partially resolves #1093) #1122

Merged
merged 6 commits into from
Dec 28, 2024

Conversation

shrirajh
Copy link
Contributor

Hey; I hope the holiday season is treating you well.

As of 3.1.0, transformers.js will result in errors when importing it via typescript (see #1093). Some of these errors are due to limitations in how typescript interprets jsdoc type declarations, and some are architectural design issues with how subclasses extending from a parent class can have methods with differing call/return signatures. (See attached error log below).

I notice #1081 is still open - that PR would make errors like this more visible, but it's still yet to be merged, and that's fine (there's a lot of changes in there to review, so I understand the wait).

In the mean time, I've applied the minimum possible fix without resorting to @ts-ignore and without any changes to architecture/design to at least get transformers.js usable via typescript.

This only partially resolves #1093, because the other errors they report with HTML elements not being found are partially issues with their own typescript config - they should add "DOM" to "lib" in their tsconfig (or transfomers.js should provide duck typed shims for HTMLCanvasElement, etc. but this is out of scope of this minimal fix PR).

Thanks!

node_modules/@huggingface/transformers/types/models/auto/processing_auto.d.ts:32:76 - error TS2304: Cannot find name 'PretrainedProcessorOptions'.

32     static from_pretrained(pretrained_model_name_or_path: string, options: PretrainedProcessorOptions): Promise<Processor>;
                                                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@huggingface/transformers/types/models/mgp_str/processing_mgp_str.d.ts:52:5 - error TS2416: Property 'batch_decode' in type 'MgpstrProcessor' is not assignable to the same property in base type 'Processor'.
  Type '([char_logits, bpe_logits, wp_logits]: Tensor[]) => { generated_text: string[]; scores: number[]; char_preds: string[]; bpe_preds: string[]; wp_preds: string[]; }' is not assignable to type '(...args: any[]) => string[]'.
    Type '{ generated_text: string[]; scores: number[]; char_preds: string[]; bpe_preds: string[]; wp_preds: string[]; }' is missing the following properties from type 'string[]': length, pop, push, concat, and 35 more.

52     batch_decode([char_logits, bpe_logits, wp_logits]: import("../../utils/tensor.js").Tensor[]): {  
       ~~~~~~~~~~~~

node_modules/@huggingface/transformers/types/models/phi3_v/processing_phi3_v.d.ts:11:70 - error TS2339: 
Property 'padding' does not exist on type 'any[]'.

11     _call(text: string | string[], images?: RawImage | RawImage[], { padding, truncation, num_crops, 
}?: any[]): Promise<any>;
                                                                        ~~~~~~~

node_modules/@huggingface/transformers/types/models/phi3_v/processing_phi3_v.d.ts:11:79 - error TS2339: 
Property 'truncation' does not exist on type 'any[]'.

11     _call(text: string | string[], images?: RawImage | RawImage[], { padding, truncation, num_crops, 
}?: any[]): Promise<any>;
                                                                                 ~~~~~~~~~~

node_modules/@huggingface/transformers/types/models/phi3_v/processing_phi3_v.d.ts:11:91 - error TS2339: 
Property 'num_crops' does not exist on type 'any[]'.

11     _call(text: string | string[], images?: RawImage | RawImage[], { padding, truncation, num_crops, 
}?: any[]): Promise<any>;
                                                                                             ~~~~~~~~~  

[11:34:32 am] Found 5 errors. Watching for file changes.

…/returns

Resolves typescript inference errors in `models/mgp_str/processing_mgp_str.js` where it does extend from `Processor` but the override returns an object instead of a string array.
…ocessor.from_pretrained`

Resolves an issue with tsc where using `@type {typeof Processor.from_pretrained}` does not actually import the `PretrainedProcessorOptions` type alias defined in `processing_utils.js`.
Resolves a typescript error where typescript tries to destructure an array directly into padding/truncation/num_crops. Also improves the type safety of calling this function.
@xenova
Copy link
Collaborator

xenova commented Dec 28, 2024

Thanks for this! Reviewing :)

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova xenova merged commit d874c90 into huggingface:main Dec 28, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3.1.2 typescript build errors
3 participants