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

The inferred type of this is wrong #8913

Closed
basarat opened this issue Jun 1, 2016 · 4 comments
Closed

The inferred type of this is wrong #8913

basarat opened this issue Jun 1, 2016 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@basarat
Copy link
Contributor

basarat commented Jun 1, 2016

On this file

declare var require:any;

var HTMLDOMPropertyConfig = require('react/lib/HTMLDOMPropertyConfig');

// Populate property map with ReactJS's attribute and property mappings
// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr
for (var propname in HTMLDOMPropertyConfig.Properties) {
  if (!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname)) {
    continue;
  }

  var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase();
}

/**
 * Repeats a string a certain number of times.
 * Also: the future is bright and consists of native string repetition:
 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
 *
 * @param {string} string  String to repeat
 * @param {number} times   Number of times to repeat string. Integer.
 * @see http://jsperf.com/string-repeater/2
 */
function repeatString(string, times) {
  if (times === 1) {
    return string;
  }
  if (times < 0) { throw new Error(); }
  var repeated = '';
  while (times) {
    if (times & 1) {
      repeated += string;
    }
    if (times >>= 1) {
      string += string;
    }
  }
  return repeated;
}

/**
 * Determine if the string ends with the specified substring.
 *
 * @param {string} haystack String to search in
 * @param {string} needle   String to search for
 * @return {boolean}
 */
function endsWith(haystack, needle) {
  return haystack.slice(-needle.length) === needle;
}

/**
 * Trim the specified substring off the string. If the string does not end
 * with the specified substring, this is a no-op.
 *
 * @param {string} haystack String to search in
 * @param {string} needle   String to search for
 * @return {string}
 */
function trimEnd(haystack, needle) {
  return endsWith(haystack, needle)
    ? haystack.slice(0, -needle.length)
    : haystack;
}

/**
 * Convert a hyphenated string to camelCase.
 */
function hyphenToCamelCase(string) {
  return string.replace(/-(.)/g, function(match, chr) {
    return chr.toUpperCase();
  });
}

/**
 * Determines if the specified string consists entirely of whitespace.
 */
function isEmpty(string) {
   return !/[^\s]/.test(string);
}

/**
 * Determines if the CSS value can be converted from a
 * 'px' suffixed string to a numeric value
 *
 * @param {string} value CSS property value
 * @return {boolean}
 */
function isConvertiblePixelValue(value) {
  return /^\d+px$/.test(value);
}

export class HTMLtoJSX {
    private output: string;
    private level: number;
    private _inPreTag: boolean;


  /**
   * Handles processing of the specified text node
   *
   * @param {TextNode} node
   */
  _visitText = (node) => {
    var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase();
    if (parentTag === 'textarea' || parentTag === 'style') {
      // Ignore text content of textareas and styles, as it will have already been moved
      // to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively.
      return;
    }

    var text = ''

    if (this._inPreTag) {
      // If this text is contained within a <pre>, we need to ensure the JSX
      // whitespace coalescing rules don't eat the whitespace. This means
      // wrapping newlines and sequences of two or more spaces in variables.
      text = text
        .replace(/\r/g, '')
        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {
          return '{' + JSON.stringify(whitespace) + '}';
        });
    } else {
      // If there's a newline in the text, adjust the indent level
      if (text.indexOf('\n') > -1) {
      }
    }
    this.output += text;
  }



};

/**
 * Handles parsing of inline styles
 */
export class StyleParser {
  styles = {};
  toJSXString = () => {
    for (var key in this.styles) {
      if (!this.styles.hasOwnProperty(key)) {
      }
    }
  }
}

which is actually a part of a larger file here

With TypeScript nightly we get the following errors:

C:/repos/alm/tests/success/simple/bas.tsx:141 Property 'styles' does not exist on type 'HTMLtoJSX'.
C:/repos/alm/tests/success/simple/bas.tsx:142 Property 'styles' does not exist on type 'HTMLtoJSX'.

Basically this in the class function isn't inferring to StyleParser and instead inferring to HTMLtoJSX

export class StyleParser {
  styles = {};
  toJSXString = () => {
    for (var key in this.styles) {
      if (!this.styles.hasOwnProperty(key)) {
      }
    }
  }
}

Sorry for not being able to narrow it down, deleting portions of the file (e.g. the HTMLtoJSX._visitText method) makes the error go away. Note that this error only appeared in the last 10 days 🌹

@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 1, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Jun 1, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jun 1, 2016

@sandersn can you take a look.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 1, 2016

possibly caused by #8849

@sandersn
Copy link
Member

sandersn commented Jun 1, 2016

#8849 contains the only change to checkThisExpression in the last month or two. It's probably the culprit. It now passes /*includeOuterFunctions*/ true to getFlowTypeOfReference. Not sure why this would cause this bug. I'll have to investigate.

@ahejlsberg
Copy link
Member

The issue is that the binder is not constructing separate control flows for property initializers. It's an easy fix. I can put up a PR.

@ahejlsberg ahejlsberg assigned ahejlsberg and unassigned sandersn Jun 2, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jun 2, 2016
basarat added a commit to alm-tools/alm that referenced this issue Jun 2, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants