2017-02-25 3 views
0

flowtype에서 다음과 같은 오류를보고하는 이유는 무엇이며 어떻게 예상대로 작동하는지 문서화해야합니까?간단한 생성자 함수의 플로우 유형 오류

index.js:7 
    4: console.log(MY_OBJECT.getName()); 
          ^^^^^^^ property `getName`. Property not found in 
    4: console.log(MY_OBJECT.getName()); 
       ^^^^^^^^^ new object 

하는 index.js

// @flow 
import {MyObject} from './object'; 
const MY_OBJECT = new MyObject('name'); 
console.log(MY_OBJECT.getName()); 

object.js :

// @flow 
export function MyObject(name: string) { 
    this._name = name; 
    this.getName = function(): string {return this._name;}; 
    this.setName = function (name: string) {this._name = name;}; 
} 

답변

1

난 그냥 알아 낸 그 것을 실제로 이것을 명시 적으로 반환 할 때 작동합니다.

// @flow 
export function MyObject(name: string) { 
    this._name = name; 
    this.getName = function(): string {return this._name;}; 
    this.setName = function (name: string) {this._name = name;}; 
    return this; 
} 
2

흐름이 스타일을 좋아하지 않는다. 같은 모듈에서 사용하면 작동하지만 다른 파일에서 가져올 때는 그렇지 않습니다.

사용하기 좋습니다 ES2015 class syntax 대신 :

// @flow 
export class MyObject { 
    name: string; 

    constructor(name: string){ 
     this.name = name; 
    } 

    getName() { 
     return this.name; 
    } 

    setName(name: string) { 
     this.name = name; 
    } 
} 

이 마음에 들지 않으면, 당신은 has limited support 것을, 프로토 타입을 사용할 수 있습니다

// @flow 
export function MyObject(name: string) { 
    this._name = name;   
} 

MyObject.prototype.getName = function(): string {return this._name;}; 
MyObject.prototype.setName = function (name: string) {this._name = name;};