Skip to content

Why is a required prop's type "T | undefined" with strictNullChecks enabled? #847

@aradalvand

Description

@aradalvand

Say you have the following component (it's using TypeScript, of course):

MyComponent.svelte:

<script lang="ts">
    export let label: string;
</script>
...

As you can see, the label prop is required, I have not supplied a default value and thus it's considered a required prop.
Now, if I have strictNullChecks set to true in my tsconfig.json file, I expect a required component prop like label in this case to not be able to receive null or undefined unless I explicitly specify that in their type.

tsconfig.json:

"compilerOptions": {
    "strictNullChecks": true
}

However, Svelte appears to make the type of the label prop string | undefined behind the scenes, and that means the consumer of this component could pass undefined as the value of label, which obviously defeats the whole purpose of enabling strictNullChecks in the first place.

<MyComponent label={undefined} /> <!-- No error! Even though "strictNullChecks" is "true" + the type of "label" is "string" -->

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedFixed in master branch. Pending production release.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions