Skip to content

TS2448 used before its declaration error for recursive function  #22975

@mohsen1

Description

@mohsen1

TypeScript Version: 2.9

Search Terms:
Control flow, recursive functions, used before its declaration.

Code
Live in the wild
https://github.com/webpack/webpack/blob/8498fc091e5375c7c41630723e8fa7916fcc1511/lib/util/cachedMerge.js#L1

const mergeCache = new WeakMap();

function cachedMerge(first, ...args) {
	if (args.length === 0) return first;
	if (args.length > 1) {
		return cachedMerge(first, cachedMerge(...args));
	}
	const second = args[0];
	let innerCache = mergeCache.get(first);
	if (innerCache === undefined) {
		innerCache = new WeakMap();
		mergeCache.set(first, innerCache);
	}
	const cachedMerge = innerCache.get(second);
	if (cachedMerge !== undefined) return cachedMerge;
	const newMerge = Object.assign({}, first, second);
	innerCache.set(second, newMerge);
	return newMerge;
};

Expected behavior:
No Error
Actual behavior:


lib/util/cachedMerge.js:12:10 - error TS2448: Block-scoped variable 'cachedMerge' used before its declaration.

12      return cachedMerge(first, cachedMerge(...args));
               ~~~~~~~~~~~


lib/util/cachedMerge.js:12:29 - error TS2448: Block-scoped variable 'cachedMerge' used before its declaration.

12      return cachedMerge(first, cachedMerge(...args));
                                  ~~~~~~~~~~~

Playground Link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions