Skip to content

[compiler/language-service::getScriptVersion] Shouldn't this statement use optional chaining and not non-null assertion operator? #1639

@cp79shark

Description

@cp79shark

const version = memoryCache.files.get(normalizedFileName)!.version

If the file is not in the MemoryCache this will throw. I'm not sure if this is the intended behavior, but it is causing issues for me.

// We need to return `undefined` and not a string here because TypeScript will use
// `getScriptVersion` and compare against their own version - which can be `undefined`.
// If we don't return `undefined` it results in `undefined === "undefined"` and run
// `createProgram` again (which is very slow). Using a `string` assertion here to avoid
// TypeScript errors from the function signature (expects `(x: string) => string`).
return version === undefined ? ((undefined as any) as string) : String(version)

The return statement, and the lengthy comment above it, already expects as much is a possibility and will return undefined.

Changing to const version = memoryCache.files.get(normalizedFileName)?.version; will allow a cache miss and return undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions