简体   繁体   English

带有打字稿的 create-react-app 将函数编译为非常奇怪的东西

[英]create-react-app with typescript compiles functions to something very strange

I've encountered some very, very strange error in my create-react-app application ( react-scripts@2.1.1 ) with typescript (at this point and have no idea if it's relevant or not - error persists through even if I rewrite affected files to plain vanilla js):我在使用react-scripts@2.1.1 create-react-app 应用程序( react-scripts@2.1.1 )中遇到了一些非常非常奇怪的错误(此时我不知道它是否相关 - 即使我重写,错误仍然存​​在)受影响的文件到普通的 vanilla js):

I have a very basic plain utility functions to compose action types for redux:我有一个非常基本的普通实用程序函数来为 redux 组合动作类型:

// ...../utils.ts
export type Type = string;
export type Types = {
  [key: string]: Type;
};

export const typeWithPrefix = (prefix: string, type: string): string => {
  return `${prefix}/${type}`;
};

export const typesWithPrefix = (prefix: string, types: Array<string>): Types => {
  return types.reduce((result: Types, type) => {
    result[type] = typeWithPrefix(prefix, type);

    return result;
  }, {});
};

export const asyncTypes = (type: string): Array<string> => {
  return [
    type,
    `${type}_START`,
    `${type}_SUCCESS`,
    `${type}_FAIL`,
    `${type}_FINALLY`
   ];
};

Usage:用法:

// ....auth/types.ts
import { typesWithPrefix, asyncTypes } from '../../utils';

export default typesWithPrefix('auth', [
    ...asyncTypes('SIGN_IN'), 
    ...asyncTypes('SIGN_OUT')
]);

// nothing fancy, plain object:
// => {SIGN_IN: 'auth/SIGN_IN', SIGN_IN_START: 'auth/SIGN_IN_START'....}

In development it works pretty much as you would expect - some utils are imported, plain object with types constants exported;在开发中,它的工作原理与您预期的差不多 - 一些实用程序是导入的,带有导出类型常量的普通对象;

But as soon as I build application with react-scripts build I see strange thing in browser console:但是,一旦我使用react-scripts build构建应用程序,我就会在浏览器控制台中看到奇怪的事情:

Uncaught TypeError: Object(...) is not a function

Inspecting compiled and minified source shows that asyncTypes is actually an Object - see screenshot .检查编译和缩小的源代码表明asyncTypes实际上是一个对象 -见截图 As far as my understanding goes in build time one of compilers or minifies decided to fold function calls to constants but how and why - beyond me, especially without ejecting from react-scripts...就我在构建时的理解而言,编译器或缩小程序之一决定将函数调用折叠为常量,但如何以及为什么 - 超出我的范围,尤其是没有从反应脚本中弹出......

So if any of you have any ideas what the hell is going on and how it can be avoided - I would be very, very glad to hear because frankly I'm out of ideas..因此,如果你们中的任何人对到底发生了什么以及如何避免它有任何想法 - 我会非常非常高兴听到,因为坦率地说我没有想法..

Well, since nobody has any clue I'll answer close this one since I did solve the problem with build but I am not sure how and why:好吧,由于没有人有任何线索,我会回答关闭这个,因为我确实解决了构建问题,但我不确定如何以及为什么:

turns out code above结果上面的代码

// ....auth/types.ts
import { typesWithPrefix, asyncTypes } from '../../utils';

typesWithPrefix(// ...

does work in development but does not in production;在开发工作但不在生产中;

but the code但代码

// ....auth/types.ts
import * as utils from '../../utils';

utils.typesWithPrefix(// ...

does works both in development and production, despite the fact thats just pure functions as named exports and reexports in index.ts files.确实适用于开发和生产,尽管事实上这只是index.ts文件中命名导出和index.ts导出的纯函数。

I would love to dig deeper and uncover this mystery but this will require to eject and I really, really don't won't to do it until I can avoid it...我很想深入挖掘并揭开这个谜团,但这需要弹出,我真的,真的不会这样做,直到我可以避免它......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM