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

Infer type from variable **write** usages #104

Open
zardoy opened this issue Feb 18, 2023 · 0 comments
Open

Infer type from variable **write** usages #104

zardoy opened this issue Feb 18, 2023 · 0 comments

Comments

@zardoy
Copy link
Owner

zardoy commented Feb 18, 2023

Default infer type from usage codefix provide too widened type, most of the time because it also infers possible types from argument usages:

Worst case example:

// @filename: foo.ts
type Options = {
    port?: number,
    host?: string,
    https?: boolean,
    key?: string,
}

type Data = string | number | boolean | undefined | null;

declare function computeThings(data?: Data | Options)
declare function computeThings(data?: Data, options?: Options)

export { computeThings }
// @filename: bar.ts
import { computeThings } from './foo'

// infer type code fix here
let data
let condition = true

if (condition) {
    data = 'John'
} else {
    data = 123
}

computeThings(data)

Expected:

let data: string | number | undefined

Actual:

let data: string | number | boolean | { port?: number | undefined; host?: string | undefined; https?: boolean | undefined; key?: string | undefined } | null | undefined

I've personally seen this in function which options has 100x more properties.

Real world example:

let replaceRanges/*: vscode.Range[] | vscode.Position[] | undefined*/ /* but gives more verbose variant */ 
if (replaceAlgorithm === 'just-replace') {
    replaceRanges = rangesToReplace
} else if (useNewVscodeInsertHack) {
    replaceRanges = editor.selections.map(selection => selection.start.translate(undefined, -snippet.sequence.length))
}

await editor.insertSnippet(
    new vscode.SnippetString(body),
    replaceRanges,
)

We will be provide additional codefix below current one with this fixed behavior, just like we provide additional extract to function refactorings.

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

No branches or pull requests

1 participant