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

Add '| undefined' to optional types in generated .d.ts #1201

Merged
merged 1 commit into from
Jan 23, 2019

Conversation

rhysd
Copy link
Contributor

@rhysd rhysd commented Jan 23, 2019

Hi,

#[wasm_bindgen]
struct X {
    pub foo: Option<i32>,
}

#[wasm_bindgen]
impl X {
    pub fn f(x: Option<i32>) -> Option<u32> {
        x.map(|i| i as u32)
    }
}

These option values generate TS type definition in .d.ts file as follows

export class X {
free(): void;
foo: number;

static  f(arg0: number): number | undefined;

}

However, by this type definition, foo and arg0 cannot accept undefined though the original values are Option when using strictNullChecks compiler flag of tsc. Passing undefined to the arg0 causes TypeScript compilation error. And the compiler thinks accessing to foo always returns non-undefined value so compiler cannot check the case where foo is undefined.

Expected type definition is:

export class X {
free(): void;
foo: number | undefined;

static  f(arg0: number | undefined): number | undefined;

}

number | undefined means a value which is number or undefined. It should make tsc happy.

This patch modifies TS type definition file generation to generate the types T | undefind for Option<T> values.

@alexcrichton alexcrichton merged commit f8dabfc into rustwasm:master Jan 23, 2019
@alexcrichton
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants