Skip to content

TS2448 used before its declaration error for recursive function  #22975

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:

Activity

  1. mhegazy commented on Mar 29, 2018

    @mhegazy
    Contributor

    that is a bug alright. but i would recommend changing the variable name to avoid the confusion.

  2. added this to the milestone on Mar 29, 2018
  3. mohsen1 commented on Apr 11, 2018

    @mohsen1
    ContributorAuthor

    Nathan Shively-Sanders (@sandersn) Daniel Rosenwasser (@DanielRosenwasser) This is the last item in todo list of migrating Webpack to use TypeScript

    webpack/webpack#6862

  4. sandersn commented on Apr 11, 2018

    @sandersn
    Member

    This error looks correct to me. If you ever run

    	if (args.length > 1) {
    		return cachedMerge(first, cachedMerge(...args));
    	}

    It will be an error. If you try the original sample in Chrome, you get a TDZ error from the const. If you run it through babel first you get an error that you can't call undefined. (Babel generates a var cacheMerged with the default settings on the REPL, at least.)

  5. mhegazy commented on Apr 11, 2018

    @mhegazy
    Contributor

    looking at it again, it is not a compiler bug, it is an actual bug. this code will not execude if the number of arguments is greater than 2.

    image

  6. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    BugA bug in TypeScript
    ES6Relates to the ES6 Spec
    on Apr 11, 2018
  7. removed this from the milestone on Apr 11, 2018
  8. locked and limited conversation to collaborators on Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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